Convert IP address to IP range - bash

I am working a project in which I am building my own network monitor and I am stuck on a bit of a problem. I was wondering how to convert a IP address to an IP address range. So far this is what I have:
# figure out the raspbery pi ip address so that can figure out ip range of network
pi_ip_addr=$(ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1 }')
# figure out ip address range based on pi ip address
ip_addr_trunk=$(echo $pi_ip_addr | cut -d . -f 1-3)
# append .{1..255} to provide a ip address based on current ip address of raspberry pi
ip_addr_range=$ip_addr_trunk".{1..255}"
So I thought this would work. However when I pass this ip_addr_range variable to another bash script that pings all IP addresses in the range it fails. The error it produces in the command line is this:
ping: unknown host 192.168.2.{1..255}
I'm guessing the problem here is when I appended the .{1..255} is somehow converting the variable to a string. Thus the loop cannot ping each IP address individually. Can anyone tell me how to successfully append the required range to the first portion of the IP address.

You want an array, not a regular parameter.
ip_addr_range=( "$ip_addr_trunk".{1..255} )
for ip_addr in "${ip_addr_range[#]}"; do
ping "$ip_addr"
done

Related

Check IP address off a list using ksh/bash

I have a list (text file) with the following data:
app1 example1.google.com
app2 example2.google.com
dev1 device1.google.com
cell1 iphone1.google.com
I want to check the ip address of the URLs/hostnames and update the text file with the gathered ip. Example:
app1 example1.google.com 192.168.1.10
app2 example2.google.com 192.168.1.55
dev1 device1.google.com 192.168.1.53
cell1 iphone1.google.com 192.168.1.199
You can use dig to get the IP (but the domains must exist). Not tested for IPv6.
#! /bin/bash
while read name url ; do
ip=$(dig -4 $url | grep '^[^;]' | grep -o '\([0-9]*[.:]\)\+[0-9.:]*$')
printf '%s %s %s\n' "$name" "$url" "$ip"
done < data.txt
If there are only two columns in the file, this might help:
awk '{"dig +short " $2 | getline ip ; print $1, $2, ip}' file
First we run a subshell (not really a good idea to run this for zillions of records) and in it a "dig +short" (the shortest possibility which came to my mind to get IP address only) with the FQDN of a machine (found in the second column). Then we print all the original columns and the new one too (with the IP address). Output can be redirected to a new file with a single >. I wouldn't consider "safe" to edit original files.

Need to grep only IP Address

nslookup google.com
Server: xx.xx.xx.xx
Address: xx.xx.xxx.xx#53
Non-authoritative answer:
Name: google.com
Address: 172.217.164.110
I just need the last IP Address with grep/awk like below, please help.
172.217.164.110
Can be enhanced, but it'll do what you want:
nslookup google.com | sed -n '/Name:/{x;n;p;d;}; x' | awk '{print $2}'
Output (when only one Name+Address block is returned by nslookup):
172.217.164.110
I used sed's pattern space advanced options, printing the line following the "Name: google.com" line (x;n;p; sequence after "Name:" pattern match). I am not a sed guru, I used this Unix Stack Exchange answer, then awk to get only the IP following a whitespace.
In a IPv6 setting, you can get both the IPv4 and the IPv6 addresses, in two lines, so if this is not what you want, you will have to filter out the IPv6, using a pattern that only matches the format of an IPv4.

How to list the assigned/leased IP adresses by DHCP server in Rapberry PI?

I am using a raspberry pi -3 and installed dnsmasq and dhcpd server for auto IP assignment.
how to know the devices connected and their IPs.
DNSMASQ leases file is located at /var/lib/dnsmasq/dnsmasq.leases
DHCPD leases file is located at /var/lib/dhcp/dhcpd.leases
In Pi we have the path of lease file is
/var/lib/misc/dnsmasq.leases
In Ubuntu it is
/var/lib/NetworkManager/*.lease
in some cases (specially with dhcpd) it is
/var/lib/dhcp/dhcpd.leases
For pi we can display the IP address of named device by
cat /var/lib/misc/dnsmasq.leases | grep "ClienDevName"|cut -d " " -f 3| awk '{print $1}'
in this command -d is for "delimiter" which is space by " " and -f 3 is to print the 3rd value after cutting with delimiter space i.e " ".

Calculate broadcast ip address using ipcalc

I have a list of ip addresses in cidr formart in a text file,one cidr per line and I would like to calculate the broadcast ip address of the cidr. I understand that I can do that using ipcalc with the -b option but ipcalc doesn't take a text file full of cidr. So, how to write a bash script to wrap around the ipcal command.
Here's a quick script that should do the trick:
Example file ips.txt
$ cat ips.txt
192.168.0.1/24
192.167.0.1/24
Example output for the ips in this file:
$ for i in $(cat ips.txt);do echo $i; ipcalc -b $i;done
192.168.0.1/24
BROADCAST=192.168.0.255
192.167.0.1/24
BROADCAST=192.167.0.255
If you don't need to output the ip address itself, just remove the echo.
Edit
Here's a version that prints only the broadcast address, as requested:
$ for i in $(cat ips.txt);do bcaddr=$(ipcalc -b $i);echo ${bcaddr#BROADCAST=};done
192.168.0.255
192.167.0.255

tcpdump - ignore unkown host error

I've got a tcpdump command running from a bash script. looks something like this.
tcpdump -nttttAr /path/to/file -F /my/filter/file
The filter file has a combination of ip addresses and host names. i.e.
host 111.111.111.111 or host 112.112.112.112 and not (host abc.com or host def.com or host zyx.com).
And it works great - as long as the host names are all valid. My problem is sometimes these hostnames will not be valid and upon encountering one - tcpdump spits out
tcpdump: Unknown Host
I thought with the -n option it would skip dns lookup - but in anycase I need it to ignore the unknown host and continue along the filter file.
Any ideas?
Thank you in advance.
The -n option prevents conversion of IP addresses into names, but not the other way around. If you supply a hostname as an argument, it has to be looked up to get the IP address since packets only contain the numeric address and not the hostname. However, there ought to be a way to ignore invalid hostnames, but I can't find one. Perhaps you could pre-process your filter file using dig.
dig +short non-existent-domain.com # returns null
dig +short google.com # returns multiple IP addresses
This could probably be better, but it should show you hostnames in your filter file that aren't valid:
grep -Po '(?<=host )[^ )]*' filterfile | grep -v '[0-9]$' | xargs -I % sh -c 'echo -n "% "; echo $(dig +short %)' | grep -v ' [0-9]'
Any hostnames it prints didn't have IP addresses returned by dig.

Resources