从IP范围ssh / bash

时间:2016-07-26 13:14:21

标签: bash awk ssh centos printf

我正在尝试在命令行中从IP地址范围(例如72.21.206.0/23)打印所有IP,更优选地使用单个命令。

我用awk&尝试了几个命令切割组合,但无法达到预期的效果。

例如,如果我在file3中有以下内容:

72.21.110.0/16
72.21.206.0/23

我希望从72.21.206.0/23中提取所有IP,然后在屏幕上以不同的行显示它们。由于我的基本知识,我才达到这一点:

awk -F'/' 'NR==2{print $1+1}' file3

应该从我的假设中打印出来但不是:

72.21.206.1

请你帮忙。

1 个答案:

答案 0 :(得分:1)

如果您有nmap可用,您可以运行类似:

nmap -n -sL 72.21.110.0/16

这将产生以下行的输出:

Nmap scan report for 72.21.0.0
Nmap scan report for 72.21.0.1
Nmap scan report for 72.21.0.2
[...]
Nmap scan report for 72.21.255.253
Nmap scan report for 72.21.255.254
Nmap scan report for 72.21.255.255
Nmap done: 65536 IP addresses (0 hosts up) scanned in 33.42 seconds

this question的答案建议使用ipcalc的解决方案。并且发现了,我想我将此标记为重复......

<强>更新

awk中的解决方案,仅适合您:

BEGIN {
  FS="/"
}

{
  split($1, octets, ".");
  base=lshift(octets[1], 24) + lshift(octets[2], 16)
    + lshift(octets[3], 8) + octets[4];
  max=lshift(1, 32-$2);

  for (i=0; i<max; i++) {
    addr = base + i;
    addr = sprintf("%s.%s.%s.%d", rshift(addr, 24),
          rshift(and(addr, 0x00FF0000), 16),
          rshift(and(addr, 0x0000FF00), 8),
          and(addr, 0xFF))
    print addr
  }
}

给出这样的输入:

$ echo 192.168.1.0/28 | awk -f ipranger.awk

你得到这样的输出:

192.168.0.0
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5
192.168.0.6
192.168.0.7
192.168.0.8
192.168.0.9
192.168.0.10
192.168.0.11
192.168.0.12
192.168.0.13
192.168.0.14
192.168.0.15
相关问题