列出分配的 dhcp 地址的命令

列出分配的 dhcp 地址的命令

是否有一个命令可以用来询问 dhcpd 服务器哪些地址已被分配?

答案1

isc-dhcpd软件包版本4.3.1有这个命令来列出租约:

dhcp-lease-list --lease PATH_TO_LEASE_FILE

这是一个简单的 Perl 脚本,也支持较旧的 DHCP 版本。您可以在以下位置看到副本Debian 源代码或在官方 DHCP 分发( 中contrib/) 也是如此。

输出很漂亮:

$ perl contrib/dhcp-lease-list.pl --lease /var/db/dhcpd/dhcpd.leases
To get manufacturer names please download http://standards.ieee.org/regauth/oui/oui.txt to /usr/local/etc/oui.txt
MAC                IP              hostname       valid until         manufacturer
===============================================================================================
90:27:e4:f9:9d:d7  192.168.0.182   iMac-de-mac    2015-12-12 01:37:06 -NA-
d8:a2:5e:94:40:81  192.168.0.178   foo-2          2015-12-12 01:04:56 -NA-
e8:9a:8f:6e:0f:60  192.168.0.127   angela         2015-12-11 23:55:32 -NA-
ec:55:f9:c5:f2:55  192.168.0.179   angela         2015-12-11 23:54:56 -NA-
f0:4f:7c:3f:9e:dc  192.168.0.183   kindle-1234567 2015-12-11 23:54:31 -NA-
f4:ec:38:e2:f9:67  192.168.0.185   -NA-           2015-12-11 23:55:40 -NA-
f8:d1:11:b7:5a:62  192.168.0.184   -NA-           2015-12-11 23:57:34 -NA-

如果您按照建议下载文件,效果会更好oui.txt,但是除非您应用以下补丁,否则输出可能会出现乱码:

--- dhcp-lease-list.pl.orig     2015-12-12 12:30:00.000000000 -0500
+++ dhcp-lease-list.pl  2015-12-12 12:54:31.000000000 -0500
@@ -41,7 +41,7 @@
     if (defined $oui) {
        $manu = join('-', ($_[0] =~ /^(..):(..):(..):/));
        $manu = `grep -i '$manu' $oui | cut -f3`;
-       chomp($manu);
+       $manu =~ s/^\s+|\s+$//g;
     }

     return $manu;
@@ -142,7 +142,7 @@
     }
     foreach (@leases) {
        if ($opt_format eq 'human') {
-          printf("%-19s%-16s%-15s%-20s%-20s\n",
+          printf("%-19s%-16s%-14.14s %-20s%-20s\n",
                  $_->{'mac'},       # MAC
                  $_->{'ip'},        # IP address
                  $_->{'hostname'},  # hostname

该补丁已作为 ISC-Bugs #41288 提交给上游并等待审核。

答案2

不可以,您只能从服务器端从 DHCP 服务器获取此信息。/var/lib/dhcpd/dhcpd.leases如果您使用的是 ISC 的 DHCP 服务器,则此信息包含在 DHCP 服务器的 .lease 文件中: 。

例子

$ more /var/lib/dhcpd/dhcpd.leases
# All times in this file are in UTC (GMT), not your local timezone.   This is
# not a bug, so please don't ask about it.   There is no portable way to
# store leases in the local timezone, so please don't request this as a
# feature.   If this is inconvenient or confusing to you, we sincerely
# apologize.   Seriously, though - don't ask.
# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-V3.0.5-RedHat

lease 192.168.1.100 {
  starts 4 2011/09/22 20:27:28;
  ends 1 2011/09/26 20:27:28;
  tstp 1 2011/09/26 20:27:28;
  binding state free;
  hardware ethernet 00:1b:77:93:a1:69;
  uid "\001\000\033w\223\241i";
}
...
...

答案3

egrep 命令可用于获取输出:

egrep "lease|hostname|hardware|\}" /var/lib/dhcpd/dhcpd.leases

输出:

lease 192.168.11.10 {
  hardware ethernet 20:6a:8a:55:19:0a;
  client-hostname "Maryam-PC";
}
lease 192.168.11.7 {
  hardware ethernet 00:16:ea:51:d3:12;
  client-hostname "parsoon";
}
lease 192.168.11.3 {
  hardware ethernet 00:17:c4:3f:84:e3;
  client-hostname "zahra-ubuntu";
}
lease 192.168.11.5 {
  hardware ethernet 58:b0:35:f1:31:2f;
}

答案4

租赁文件的格式已更改,或者至少在使用dhcpcd5.要查看您拥有的wlan0WiFi 网络租约MyNetwork,您必须查看此文件(或类似文件):/var/lib/dhcpcd5/dhcpcd-wlan0-MyNetwork.lease

该文件是一个二进制文件。 (为什么?我不知道。也许是为了在解析它时节省一些宝贵的 CPU 周期?Blech。)要查看它,请使用dhcpcd --dumplease,它从 STDIN 解析二进制文件并输出人类可读的版本:

cat /var/lib/dhcpcd5/dhcpcd-wlan0-MyNetwork.lease | dhcpcd --dumplease

另一方面,如果您只想查看当前分配的租约wlan0是什么,您可以简单地执行以下操作:

dhcpcd --dumplease wlan0

相关内容