码迷,mamicode.com
首页 > 其他好文 > 详细

统计dhcpd.lease IP 地址

时间:2015-04-20 18:49:48      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:python dhcpd.release

#!/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


本文出自 “Smurf Linux运维” 博客,请务必保留此出处http://jin771998569.blog.51cto.com/2147853/1636207

统计dhcpd.lease IP 地址

标签:python dhcpd.release

原文地址:http://jin771998569.blog.51cto.com/2147853/1636207

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!