如何以破折号格式扩展IP地址?

Wol*_*olf 5 shell-script

样本数据

10.1.1.1-10.1.1.3
10.100.100.11-10.100.100.15
Run Code Online (Sandbox Code Playgroud)

Linux 中是否有任何可用的技巧可以将此 ip 扩展为以下格式?

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
Run Code Online (Sandbox Code Playgroud)

我知道有在线工具,例如https://techzoom.net/lab/ip-address-calculator/,但我想编写脚本而不是使用在线工具。

让我知道是否有解决方案(与什么工具、bash、python 或任何东西无关)

Sté*_*las 6

使用perlsNet::IP模块(libnet-ip-perl基于 Debian 的系统中的包):

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
Run Code Online (Sandbox Code Playgroud)


小智 4

可惜 nmap 不支持10.1.1.1-10.1.1.3格式。

$ 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
$ 
Run Code Online (Sandbox Code Playgroud)

但是,如果您可以删除第 2 个第 3 个八位字节,并使其像10.1.1.1-3,那么只需使用带 -sL 选项的 nmap

$ 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
$ 
Run Code Online (Sandbox Code Playgroud)