我已经在 CentOS 6.5 上安装并配置了 DHCP 服务器。我还在dhcpd.conf
文件中添加了一个子网,如下所示:
subnet 192.168.1.0 netmask 255.255.255.0 {
option domain-name-servers 192.168.1.2, 8.8.8.8;
default-lease-time 600;
max-lease-time 7200;
range dynamic-bootp 192.168.1.10 192.168.1.30;
option broadcast-address 192.168.1.255;
option routers 192.168.1.1;
option ip-forwarding off;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,DHCP 服务器只能分配 20 个 IP 地址。是否可以让 DHCP 服务器在使用 shell 脚本分配所有 20 个地址后向系统管理员发送警报?
您可以选择计算dhcpd.leases 中的lease
声明数量:
dhcpd.leases(5) - Linux man page
Name
dhcpd.leases - DHCP client lease database
....
the Lease Declaration
lease ip-address { statements... }
Each lease declaration includes the single IP address that has been leased to the
client. The statements within the braces define the duration of the lease and to
whom it is assigned.
Run Code Online (Sandbox Code Playgroud)
因此,您只需计算开头的行数lease
即可知道已分配的 ip 地址数:
COUNT=$(grep -c '^lease' /var/lib/dhcpd/dhcpd.leases)
if [[ $COUNT eq 20 ]]
then
#do something here
fi
Run Code Online (Sandbox Code Playgroud)
这不是一个直接的解决方案,但您似乎可以利用on commit
DHCP 配置文件中的工具。这是这篇题为:当 ISC DHCP分发新租约时执行脚本的文章中的一个示例。
在该dhcpd.conf
文件中,您可以针对各种事件创建操作,例如何时发出租约。
subnet 192.168.1.0 netmask 255.255.255.0 {
option routers 192.168.1.2;
on commit {
set clip = binary-to-ascii(10, 8, ".", leased-address);
set clhw = binary-to-ascii(16, 8, ":", substring(hardware, 1, 6));
execute("/usr/local/sbin/dhcpevent", "commit", clip, clhw, host-decl-name);
}
...
Run Code Online (Sandbox Code Playgroud)
当上面的脚本 ,dhcpevent
运行时,它传递了 4 个参数。
execute_statement argv[0] = /usr/local/sbin/dhcpevent
execute_statement argv[1] = commit
execute_statement argv[2] = 192.168.1.40
execute_statement argv[3] = 11:aa:bb:cc:dd:ee
execute_statement argv[4] = d1.jp
Run Code Online (Sandbox Code Playgroud)
clipw
&clhw
是变量,在此示例中,其他元数据的一部分在运行脚本之前已被解析和存储。这些变量然后与其他项目一起传递给事件脚本。
您可以将此方法运用到一个脚本中,您可以在其中跟踪已租出的 IP 数量,或者您可以询问 DHCP 服务器跟踪此信息的实际租用状态文件 ( /var/lib/dhcpd/dhcpd.leases
),然后报告是否存在文件的租用数量超过了您的配额。