#!/usr/bin/env python
# coding=utf-8
import string
import time,datetime
class TIMEFORMAT:
def __init__(self, time_string="1970-1-1 00:00:00"):
self.time_string = self._format_time_string(time_string)
def _format_time_string(self, time_string):
return time.strftime("%Y-%m-%d %H:%M:%S", self.get_struct(time_string))
@property
def time_struct(self):
return self.get_struct(self.time_string)
def get_struct(self, time_string):
return time.localtime(self.get_seconds(time_string))
@property
def seconds(self):
return self.get_seconds(self.time_string)
def get_seconds(self, time_string):
d = datetime.datetime.strptime(time_string, "%Y-%m-%d %H:%M:%S")
return time.mktime(d.timetuple())
def get_string(self, time_sec):
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time_sec))
# 对于中国的时间,是1970-01-01 08:00:00
def check_diff_time(self, t1, t2):
sec1 = int(self.get_seconds(t1))
sec2 = int(self.get_seconds(t2))
if sec1 > sec2:
secdiff = sec1 - sec2
else:
secdiff = sec2 - sec1
d = self.get_struct(self.get_string(secdiff))
day = d.tm_mday
hour = d.tm_hour
if d.tm_hour < 8:
day -= 1
hour = 24 + (d.tm_hour - 8)
else:
hour = d.tm_hour - 8
return {
"year" :d.tm_year - 1970,
"month" :d.tm_mon - 1,
"day" : day - 1,
"hour" : hour,
"min" : d.tm_min,
"sec" : d.tm_sec,
}
#######################################
alist=[]
lease_IP=' '
lease_start=' '
lease_end=' '
istatus=' '
MAC=' '
client_Hostname=' '
# 修改以下路径
f=open('/var/lib/dhcpd/dhcpd.leases')
lines = f.readlines()
f.close()
#########################################
for line in lines:
if line.find('lease') <> -1:
lease_IP = line.split('\n')[0].split(' ')[1]
if line.find('starts') <> -1:
lease_start = line.split('\n')[0].split(' ')[4:6]
if line.find('ends') <> -1:
lease_end = line.split('\n')[0].split(' ')[4:6]
if line.find('binding state active') <> -1:
istatus='active'
if line.find('next binding state') <> -1:
pass
if line.find('hardware ethernet') <> -1:
MAC=line.split('\n')[0].split(' ')[4].split(';')[0]
if line.find('uid') <> -1:
pass
if line.find('client-hostname') <> -1:
if istatus == 'active':
client_Hostname= line.split('\n')[0].split(' ')[3].split('"')[1]
start_time = str(lease_start[0]) +" " + str(lease_start[1]).rstrip(';')
end_time = str(lease_end[0]) + " " + str(lease_end[1]).rstrip(';')
start_time_format = time.strftime("%Y-%m-%d %H:%M:%S",time.strptime(start_time,"%Y/%m/%d %H:%M:%S"))
end_time_format = time.strftime("%Y-%m-%d %H:%M:%S",time.strptime(end_time,"%Y/%m/%d %H:%M:%S"))
t1 = TIMEFORMAT("%s"%(start_time_format))
t2 = TIMEFORMAT("%s"%(end_time_format))
d = t1.check_diff_time(t1.time_string, t2.time_string)
diff = str(d["year"]) + "年" + str(d["month"]) + "月" + str(d["day"]) + "天 " + str(d["hour"])+ "时" + str(d["min"]) + "分" + str(d["sec"]) + "秒"
record = str(lease_IP) +"\t" + start_time + "\t" + end_time + "\t" + diff + "\t" + str( MAC )+ "\t" + str(istatus) + "\t" + str( client_Hostname )
alist.append(record)
lease_IP = ' '
lease_start = ' '
lease_end = ' '
istatus = ' '
MAC = ' '
client_Hostname =' '
else:
pass
print "IP 地址" + "\t\t\t" + "获取时间" + "\t\t\t\t" + "释放时间" + "\t\t\t\t" + " 剩余时间" + "\t\t\t\t" +"MAC地址" + "\t\t\t\t" +"是否激活" + "\t" + "主机名"
for ip in alist:
print ip
![分析dhcp.lease文件,统计DHCP服务器IP自动分配 分析dhcp.lease文件,统计DHCP服务器IP自动分配](https://image.shishitao.com:8440/aHR0cHM6Ly9pbWcyMDE4LmNuYmxvZ3MuY29tL2Jsb2cvMTA3NDk3MS8yMDE4MTAvMTA3NDk3MS0yMDE4MTAxMjExMzAxMDk0MS0xNTUxOTU5MDM0LnBuZw%3D%3D.png?w=700&webp=1)
提取IP和主机名,并排序
./print_ip.py | awk 'NR!=1{print $1,$5}' OFS="\t\t" | sort -u -t. -k3,3n -k4,
![分析dhcp.lease文件,统计DHCP服务器IP自动分配 分析dhcp.lease文件,统计DHCP服务器IP自动分配](https://image.shishitao.com:8440/aHR0cHM6Ly9pbWcyMDE4LmNuYmxvZ3MuY29tL2Jsb2cvMTA3NDk3MS8yMDE4MTEvMTA3NDk3MS0yMDE4MTEwNjE2NTEyODIyMC0zMjQwODkwOC5wbmc%3D.png?w=700&webp=1)