Alan
Alan

Reputation: 163

Available IPs list

I have list of IPs and their respective hostname allocations like this:

192.168.1.1 - GW          
192.168.1.2 - HOSTA 
192.168.1.3 - HOSTB        
192.168.1.7 - HOSTC

The list is big. More than 4000 rows with different subnets.

I want to extract via BASH the available IPs which are the ones that in the above list.

For instance, IPs:

192.168.1.4 
192.168.1.5 
192.168.1.6

To accomplish I'm trying to compare the IP numbers of the last octect with a {1-255} list. If the numbers of the last octect is not in list then the IP is available.

Any other ideas?

Upvotes: 2

Views: 148

Answers (1)

Chris Seymour
Chris Seymour

Reputation: 85785

With awk you could do:

$ awk 'function f(){while(++a!=$4&&a<257)print IP,a}
       a+1!=$4&&NR>1{f()}{a=$4;IP=$1OFS$2OFS$3}END{f()}' FS='[. ]' OFS=. file

This will print

192.168.1.4 
192.168.1.5 
192.168.1.6
192.168.1.8
192.168.1.9
192.168.1.10
...
192.168.1.254
192.168.1.255
192.168.1.256

If you want to to treat the last IP 192.168.1.7 as the upper limit and not print the available IPs above just remove the END block END{f()}.

Upvotes: 2

Related Questions