请使用grep命令查找IP地址范围


1

我需要从租约文件中找到一系列的IP地址。我想用这个

less / var / lib / dhcpd / | grep说范围从192.23.253.2到192.23.253.100

什么命令对此有用


1
链接 ARIN结果为192.23 </A>显示了Schlumberger IT安全运营。更通用的选项包括192.0.2或198.51.100或203.0.113(根据IETF RFC 3330和IETF RFC 5735),或者可能是IETF BCP 5(RFC 1918)地址(如moonbutt74的答案所示)。
TOOGAM 2015年

Answers:


3

我觉得这awk是一个更灵活的工具来完成这项任务。Python也可以使用,但你必须编写脚本而不是复制和粘贴终端命令。您将不得不更改正则表达式模式,记录分隔符和字段分隔符,以便它们可以处理您的文件。如果您可以提供文件中的示例(删除或更改敏感数据),我将编辑此答案。

测试文件(从另一个SO示例复制并调整):

lease 192.23.253.2 {
    starts 6 2009/06/27 00:40:00;
    ends 6 2009/06/27 12:40:00;
    hardware ethernet 00:00:00:00:00:00;
    uid 00:00:00:00:00:00;
    client-hostname "examle-workstation1";
}
lease 192.23.253.3 {
    starts 6 2009/06/27 00:40:00;
    ends 6 2009/06/27 12:40:00;
    hardware ethernet 00:00:00:00:00:00;
    uid 00:00:00:00:00:00;
    client-hostname "examle-workstation1";
}
lease 192.23.253.4 {
    starts 6 2009/06/27 00:40:00;
    ends 6 2009/06/27 12:40:00;
    hardware ethernet 00:00:00:00:00:00;
    uid 00:00:00:00:00:00;
    client-hostname "examle-workstation1";
}
lease 192.23.253.5 {
    starts 6 2009/06/27 00:40:00;
    ends 6 2009/06/27 12:40:00;
    hardware ethernet 00:00:00:00:00:00;
}
lease 192.23.253.6 {
    starts 6 2009/06/27 00:40:00;
    ends 6 2009/06/27 12:40:00;
    hardware ethernet 00:00:00:00:00:01;
    uid 00:00:00:00:00:01;
    client-hostname "examle-workstation2";
}
lease 192.23.253.7 {
    starts 6 2009/06/27 00:40:00;
    ends 6 2009/06/27 12:40:00;
    hardware ethernet 01:00:00:00:00:00;
}

如果你想要整个记录:

$ awk 'BEGIN{
      RS="lease"
      FS=" {+|;\n"
  }{
      n=split($1, a, ".")
      last=int(a[n])
      if( 3 <= last && last <= 6){
          print
      }
  }' testfile

awk声明中发生了什么:

  • 设置记录分隔符值:RS="lease"
  • 设置字段分隔符值以查找{空格或;\nFS=" {+|;\n"
  • 分割的第一个字段的每个记录在任何.,存储所得数组中a,存储的长度an
  • 存储ain 的最后一个元素last
  • 测试if last是否等于或小于我们的最小值(2)或者如果last等于或大于我们的最小值(6),如果是,则打印整个记录。

结果:

 192.23.253.3 {
    starts 6 2009/06/27 00:40:00;
    ends 6 2009/06/27 12:40:00;
    hardware ethernet 00:00:00:00:00:00;
    uid 00:00:00:00:00:00;
    client-hostname "examle-workstation1";
}

192.23.253.4 {
    starts 6 2009/06/27 00:40:00;
    ends 6 2009/06/27 12:40:00;
    hardware ethernet 00:00:00:00:00:00;
    uid 00:00:00:00:00:00;
    client-hostname "examle-workstation1";
}

192.23.253.5 {
    starts 6 2009/06/27 00:40:00;
    ends 6 2009/06/27 12:40:00;
    hardware ethernet 00:00:00:00:00:00;
}

192.23.253.6 {
    starts 6 2009/06/27 00:40:00;
    ends 6 2009/06/27 12:40:00;
    hardware ethernet 00:00:00:00:00:01;
    uid 00:00:00:00:00:01;
    client-hostname "examle-workstation2";
}

如果只需要IP地址:

$ awk 'BEGIN{
     RS="lease"
     FS=" {+|;\n"
  }{
      n=split($1, a, ".")
      last=int(a[n])
      if( 3 <= last && last <= 6){
          ip=gensub(/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*/,"\\1","", $1)
          print ip
      }
  }' testfile

awk声明中发生了什么:

  • 设置记录分隔符值:RS="lease"
  • 设置字段分隔符值以查找{空格或;\nFS=" {+|;\n"
  • 分割的第一个字段的每个记录在任何.,在存储所产生的阵列a,存储的长度an
  • 存储ain 的最后一个元素last
  • 测试if last是否等于或小于我们的最小值(2)或者如果last等于或大于我们的最小值(6),如果这是真的,使用该gensub方法使用正则表达式模式除去IP地址以外的所有内容。

结果:

192.23.253.3
192.23.253.4
192.23.253.5
192.23.253.6

1

如果我正确理解你的问题,这样的话?

root@kali:/var/lib/dhcp# sed -n -e '/20:40:42/,/12:25:01/p' dhclient-ce17152e-8364-40bd-a3d9-5d916e421dc3-wlan0.lease | grep "expire" > narc.txt && cat narc.txt
  expire 5 2015/08/14 20:40:42;
  expire 6 2015/08/15 12:25:01;

所以,

root@kali:/var/lib/dhcp# sed -n -e '/<range-from-start>/,/<range-to-end>/p' <EXACT-LOCATION-AND-FILENAME> | grep "<beginning-line-entry-of-lease-file>" > <output-file> && cat <output-file>
  expire 5 2015/08/14 20:40:42;
  expire 6 2015/08/15 12:25:01;

我的电脑文件的租约看起来像这样

lease {
  interface "wlan0";
  fixed-address 192.168.254.25;
  option subnet-mask 255.255.255.0;
  option dhcp-lease-time 86400;
  option routers 192.168.254.254;
  option dhcp-message-type 5;
  option dhcp-server-identifier 192.168.254.254;
  option domain-name-servers 192.168.254.254;
  option dhcp-renewal-time 43200;
  option dhcp-rebinding-time 75600;
  option broadcast-address 192.168.254.255;
  option host-name "kali";
  option domain-name "netgear.com";
  renew 5 2015/08/14 08:37:20;
  rebind 5 2015/08/14 17:40:42;
  expire 5 2015/08/14 20:40:42;
}
lease {
  interface "wlan0";
  fixed-address 192.168.254.25;
  option subnet-mask 255.255.255.0;
  option routers 192.168.254.254;
  option dhcp-lease-time 86400;
  option dhcp-message-type 5;
  option domain-name-servers 192.168.254.254;
  option dhcp-server-identifier 192.168.254.254;
  option dhcp-renewal-time 43200;
  option broadcast-address 192.168.254.255;
  option dhcp-rebinding-time 75600;
  option host-name "kali";
  option domain-name "netgear.com";
  renew 6 2015/08/15 00:05:34;
  rebind 6 2015/08/15 09:25:01;
  expire 6 2015/08/15 12:25:01;
}

我不认为拉数字会有效,而不会因为它们所带来的“标签”而贪图。


@Ray感谢你铺设它,看起来很整洁!
moonbutt74 2015年

1
less /var/lib/dhcpd/ | grep 192\.23\.253\. |grep -vi 192\.23\.253\.1\s |grep -vi 192\.23\.253\.2[0-9][0-9] |grep -vi 192\.23\.253\.1[0-9][1-9] |grep -vi 192\.23\.253\.1[1-9]0 |grep -vi 192\.23\.253\.0

只有grep。


不会grep -vi 1 [0-9] [1-9] $过滤掉.100吗?(此外,他确实要求的范围不同于192.168。)另外,根据moonbutt74的答案显示的输出,似乎IP地址没有出现在一行的末尾,所以使用$可能不会像你想要的那样工作。
TOOGAM 2015年

注意第二个[1-9],所以0被排除在外。我完全测试了它,100确实有效,除非我复制并粘贴错误,但我不认为我做了。我确定了范围,并将在
2015年

修正了我的回答。
2015年

0

我不认为你可以使用grep获得这样的最小值和最大值。你可能可以用一个小awk“程序”来做到这一点

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.