Dhcp

列出分配的 dhcp 地址的命令

  • August 31, 2018

有沒有我可以用來詢問 dhcpd 伺服器分配了哪些地址的命令?

不,您只能從 DHCP 伺服器獲取此資訊伺服器端。此資訊包含在 DHCP 伺服器的 .lease 文件中:/var/lib/dhcpd/dhcpd.leases如果您使用的是 ISC 的 DHCP 伺服器。

例子

$ 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";
}
...
...

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 向上游送出並等待審核。

引用自:https://unix.stackexchange.com/questions/91799