我们知道,DHCP可以动态的分配IP地址。
但是,我们怎么知道DHCP到底分配出了哪些地址?难道去网段里面去扫描?
这当然是可行的方法之一,但是,有更好的方法。
DHCPD服务,会将已经分配出去的IP的相关信息记录在一个文件里面:/var/lib/dhcpd/dhcpd.leases
具体如下:
[root@center-me ~]# cat /var/lib/dhcpd/dhcpd.leases # The format of this file is documented in the dhcpd.leases(5) manual page. # This lease file was written by isc-dhcp-4.2.5 lease 10.158.1.104 { starts 3 2017/02/15 06:43:38; ends 3 2017/02/15 12:43:38; tstp 3 2017/02/15 12:43:38; cltt 3 2017/02/15 06:43:38; binding state free; hardware ethernet 00:50:56:84:67:c1; } lease 10.158.1.103 { starts 3 2017/02/15 06:52:30; ends 3 2017/02/15 12:52:30; tstp 3 2017/02/15 12:52:30; cltt 3 2017/02/15 06:52:30; binding state free; hardware ethernet 00:50:56:84:1d:b1; } lease 10.158.1.105 { starts 3 2017/02/15 06:56:05; ends 3 2017/02/15 12:56:05; tstp 3 2017/02/15 12:56:05; cltt 3 2017/02/15 06:56:05; binding state free; hardware ethernet 00:50:56:84:00:8a; } lease 10.158.1.106 { starts 4 2017/02/16 03:00:06; ends 4 2017/02/16 09:00:06; tstp 4 2017/02/16 09:00:06; cltt 4 2017/02/16 03:00:06; binding state free; hardware ethernet 00:50:56:84:22:fa; set vendorclass = "PXEClient:Arch:00000:UNDI:002001"; } lease 10.158.1.107 { starts 5 2017/02/17 09:26:32; ends 5 2017/02/17 15:26:32; tstp 5 2017/02/17 15:26:32; cltt 5 2017/02/17 09:26:32; binding state free; hardware ethernet 00:50:56:a6:0c:5b; } lease 10.158.1.108 { starts 1 2017/02/20 03:16:36; ends 1 2017/02/20 09:16:36; tstp 1 2017/02/20 09:16:36; cltt 1 2017/02/20 03:16:36; binding state free; hardware ethernet 00:50:56:84:62:bd; uid "\001\000PV\204b\275"; set vendorclass = "MSFT 5.0"; } lease 10.158.1.109 { starts 1 2017/02/20 05:50:30; ends 1 2017/02/20 11:50:30; tstp 1 2017/02/20 11:50:30; cltt 1 2017/02/20 05:50:30; binding state free; hardware ethernet 00:50:56:84:71:71; set vendorclass = "PXEClient:Arch:00000:UNDI:002001"; } lease 10.158.1.117 { starts 5 2017/02/24 05:21:50; ends 5 2017/02/24 11:21:50; tstp 5 2017/02/24 11:21:50; cltt 5 2017/02/24 05:21:50; binding state free; hardware ethernet 00:50:56:84:00:c8; } lease 10.158.1.121 { starts 5 2017/02/24 05:41:18; ends 5 2017/02/24 11:41:18; tstp 5 2017/02/24 11:41:18; cltt 5 2017/02/24 05:41:18; binding state free; hardware ethernet 00:50:56:84:01:8e; } lease 10.158.1.116 { starts 5 2017/02/24 05:57:48; ends 5 2017/02/24 11:57:48; tstp 5 2017/02/24 11:57:48; cltt 5 2017/02/24 05:57:48; binding state free; hardware ethernet 00:50:56:84:2b:e2; } lease 10.158.1.119 { starts 5 2017/02/24 06:02:21; ends 5 2017/02/24 12:02:21; tstp 5 2017/02/24 12:02:21; cltt 5 2017/02/24 06:02:21; binding state free; hardware ethernet 00:50:56:84:5a:79; } lease 10.158.1.122 { starts 5 2017/02/24 06:10:04; ends 5 2017/02/24 12:10:04; tstp 5 2017/02/24 12:10:04; cltt 5 2017/02/24 06:10:04; binding state free; hardware ethernet 00:50:56:84:60:d9; } lease 10.158.1.101 { starts 2 2017/02/28 20:16:33; ends 3 2017/03/01 02:16:33; cltt 2 2017/02/28 20:16:33; binding state active; next binding state free; rewind binding state free; hardware ethernet 00:50:56:84:6a:7d; uid "\001\000PV\204j}"; set vendorclass = "udhcp 1.23.1"; client-hostname "boot2docker"; } lease 10.158.1.102 { starts 2 2017/02/28 23:15:25; ends 3 2017/03/01 05:15:25; cltt 2 2017/02/28 23:15:25; binding state active; next binding state free; rewind binding state free; hardware ethernet 00:50:56:02:01:00; uid "\001\000PV\002\001\000"; set vendorclass = "MSFT 5.0"; client-hostname "WIN-PVEA1SONOMA"; } server-duid "\000\001\000\001 6\240\236\000PV\204\013`"; lease 10.158.1.101 { starts 2 2017/02/28 23:16:33; ends 3 2017/03/01 05:16:33; cltt 2 2017/02/28 23:16:33; binding state active; next binding state free; rewind binding state free; hardware ethernet 00:50:56:84:6a:7d; uid "\001\000PV\204j}"; set vendorclass = "udhcp 1.23.1"; client-hostname "boot2docker"; } [root@center-me ~]# [root@center-me ~]#
可以看到,改文件中可能会有重复的记录条目。
更清楚的看到,如下:
[root@center-me ~]# cat /var/lib/dhcpd/dhcpd.leases | grep lease | cut -d' ' -f2 | grep -v -E "The|This" 10.158.1.104 10.158.1.103 10.158.1.105 10.158.1.106 10.158.1.107 10.158.1.108 10.158.1.109 10.158.1.117 10.158.1.121 10.158.1.116 10.158.1.119 10.158.1.122 10.158.1.101 10.158.1.102 10.158.1.101 [root@center-me ~]#
可以看到【101】是重复的。
要想筛选出来不重复的IP列表可以通过脚本或者一句命令实现:
[root@center-me ~]# cat /var/lib/dhcpd/dhcpd.leases | grep lease | cut -d' ' -f2 | grep -v -E "The|This" | sort -u 10.158.1.101 10.158.1.102 10.158.1.103 10.158.1.104 10.158.1.105 10.158.1.106 10.158.1.107 10.158.1.108 10.158.1.109 10.158.1.116 10.158.1.117 10.158.1.119 10.158.1.121 10.158.1.122 [root@center-me ~]#
————————————
Done。