代码之家  ›  专栏  ›  技术社区  ›  Sri

操作dhcp/conf的脚本

  •  1
  • Sri  · 技术社区  · 17 年前

    我想写一个脚本来操纵dhcp.conf文件文件。从某种意义上说,它必须读取文件,它应该ping所有的租赁IP,并应该给出另一个文本文件,其中应该给出IP列表,哪些设备现在已经存在。

    编辑:谢谢你的迅速回复。dhcpd.conf文件文件是这样的

    lease 172.31.0.10 {
      some text
      some text1
    }
    
    lease 172.31.0.12 {
      some text
      some text1
    }
    
    lease 172.31.0.100 {
      some text
      some text1
    }
    

    所以首先我需要先提取ip地址,然后一个接一个地ping

    2 回复  |  直到 17 年前
        1
  •  1
  •   derobert    17 年前

    假设

    首先,有两件事你需要注意:

    • 并非所有设备都响应ping。相当多的电脑防火墙禁用 平回答。如果你在以太网上, arping 可以用来代替 甚至可以检测到装有防火墙的电脑。
    • dhcpd在文件中留下它/知道/不再有效的租约。

    lease 192.168.66.132 {
      starts 4 2009/01/08 23:58:41;
      ends 5 2009/01/09 00:00:41;
      binding state free;
      hardware ethernet 00:e0:81:28:2d:56;
    }
    lease 192.168.66.133 {
      starts 5 2009/01/09 03:17:17;
      ends 2 2038/01/19 03:14:06;
      binding state active;
      next binding state free;
      hardware ethernet 00:e0:81:28:2d:57;
    }
    

    你可以看到132没有被使用( binding state free )133是 binding state active ). 另一种可能性是 binding state backup , 但这只发生在故障转移配置中。

    也可以放弃租约,这意味着DHCP服务器 正在分配该IP,但发现它已在使用中(通过ping)。

    dhcpd.leases(5) .

    1. DHCP服务器已重新使用过期的租约。有什么好办法吗 你为什么要检查它的工作?如果你用完了

    2. nmap -sP <start_ip>-<end_ip> 你需要什么?那也会

    重新写问题

    所以,考虑到上述情况,假设你还想要这个,我会 请回答以下问题:

    请编写一个脚本来查找所有活动的或不活动的租约 放弃并确定当前是否有使用该IP的计算机 地址。

    因此:

    #!/usr/bin/perl
    use File::Slurp qw(slurp);
    use Data::Dump qw(pp);
    use strict;
    
    1 == @ARGV
        or die "Usage: $0 dhpcd.leases\n";
    my $leases = slurp($ARGV[0]);
    $leases =~ s/^#.*\n//mg;
    my @leases = split(/lease (\d.+\d.+\d.+\d+) {/, $leases);
    shift @leases;
    my %lease = @leases;
    
    while (my ($ip, $rec) = each %lease) {
        print $ip;
        $rec =~ /^\s*abandoned;\s*$/m and print " abandoned";
        $rec =~ /^\s*binding state free;\s*$/m and print " free";
        $rec =~ /^\s*binding state active;\s*$/m and print " active";
        print "\n";
    }
    

    这个相对丑陋的perl脚本将提供如下输出:

    192.168.66.132 free
    192.168.66.133 active
    

    这对你来说应该是很容易的。

        2
  •  0
  •   Sri Sri    17 年前

    问题是, 结合态激活; 不表示设备处于活动状态。为了确保设备处于活动状态,我们需要首先ping设备。我们没有别的办法了