TransWikia.com

How to expand IP Address in dash format?

Unix & Linux Asked on February 7, 2021

Sample data

10.1.1.1-10.1.1.3
10.100.100.11-10.100.100.15

Is there any available trick in Linux to expand this ip to the following format?

10.1.1.1
10.1.1.2
10.1.1.3

10.100.100.11
10.100.100.12
10.100.100.13
10.100.100.14
10.100.100.15

I know there are online tools such as https://techzoom.net/lab/ip-address-calculator/, but I would like to script this instead of using online tool.

Let me know if there is solution for this (doesn’t matter what tool, bash, python or anything)

3 Answers

For multiple ranges:

awk -F[.-] '
  {for(i=$1;i<=$5;i++)
    for(j=$2;j<=$6;j++)
      for(k=$3;k<=$7;k++)
        for(l=$4;l<=$8;l++)
          printf("%d.%d.%d.%dn", i,j,k,l)}
' file

Or at stdin: echo "range" | awk ...
Example:

echo -n "10.0.1.10-10.1.2.12n192.168.122.0-192.168.122.3" | awk ...
10.0.1.10
10.0.1.11
10.0.1.12
10.0.2.10
10.0.2.11
10.0.2.12
10.1.1.10
10.1.1.11
10.1.1.12
10.1.2.10
10.1.2.11
10.1.2.12
192.168.122.0
192.168.122.1
192.168.122.2
192.168.122.3

Answered by nezabudka on February 7, 2021

With perls Net::IP module (libnet-ip-perl package in Debian based systems):

perl -MNet::IP -lne '
  print $an_empty_line unless $. == 1;
  my $ip = Net::IP->new($_);
  do {print $ip->ip} while (++$ip)' < file-with-ip-ranges

Answered by Stéphane Chazelas on February 7, 2021

Too bad that nmap does not support 10.1.1.1-10.1.1.3 format.

$ nmap -sL 10.1.1.1-10.1.1.3
Starting Nmap
Failed to resolve "10.1.1.1-10.1.1.3".
WARNING: No targets were specified, so 0 hosts scanned.
Nmap done: 0 IP addresses (0 hosts up) scanned in 0.04 seconds
$ 

But if you can remove the 2nd 3rd octet, and make it like 10.1.1.1-3, then just use nmap with -sL option

$ nmap -sL 10.1.1.1-3 | egrep -o "([0-9]{1,3}[.]){3}[0-9]{1,3}"
10.1.1.1
10.1.1.2
10.1.1.3
$ 

Answered by user14251572 on February 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP