分析dhcp.lease文件,統計DHCP服務器IP自動分配

#!/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
執行:python print_ip.py

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章