统计dhcpd.lease IP 地址

#!/usr/bin/env python
# coding=utf-8
import string
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]
            record = str(lease_IP) +"\t" +str(lease_start[0]) +" " +  str(lease_start[1]).rstrip(';') + "\t" + str(lease_end[0]) + " " +  str(lease_end[1]).rstrip(';') + "\t" + str( MAC )+ "\t" + str( client_Hostname )
            alist.append(record)
            lease_IP = ' '
            lease_start = ' '
            lease_end = ' '
            istatus = ' '
            MAC = ' '
            client_Hostname =' ' 
        else:
            pass
for ip in alist:
    print ip


你可能感兴趣的:(python,dhcpd.release)