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

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

我需要从我的租约文件中查找 IP 地址范围。我正在尝试使用这个

less /var/lib/dh​​cpd/ | grep 说范围是从 192.23.253.2 到 192.23.253.100

什么命令可以解决这个问题

答案1

我觉得这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,将长度存储a在中n
  • 将的最后一个元素存储a在中last
  • 测试是否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,将长度存储a在中n
  • 将的最后一个元素存储a在中last
  • 测试是否last等于或小于我们的最小值(2)或是否last等于或大于我们的最大值(6),如果为真,则使用该gensub方法使用正则表达式模式删除除 IP 地址之外的所有内容。

结果:

192.23.253.3
192.23.253.4
192.23.253.5
192.23.253.6

答案2

如果我正确理解了你的问题,是这样的吗?

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;
}

我认为,如果不对数字所属的“标签”进行 grep,只提取数字是行不通的。

答案3

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。

答案4

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

相关内容