分析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

提取IP和主机名,并排序
./print_ip.py | awk 'NR!=1{print $1,$5}' OFS="\t\t" | sort -u -t. -k3,3n -k4,

分析dhcp.lease文件,统计DHCP服务器IP自动分配的更多相关文章
- 日志文件 统计 网站PV IP
1. 安装rrdtool yum install rrdtool 2. 创建 rrdtool 数据库 rrdtool create /opt/local/rrdtool/jicki.rrd -s 30 ...
- DHCP服务自动分配IP地址原理
转载自:http://blog.csdn.net/lycb_gz/article/details/8499559 DHCP在提供服务时,DHCP客户端是以UDP 68号端口进行数据传输的,而DHCP服 ...
- 阿里云 云解析使用方法/在阿里云ESC服务器解析域名并绑定服务器IP后上传文件通过域名访问步骤教程
第一步:登录阿里云官网,获取服务器ECS的指定公网IP地址. 1.输入阿里云官网账号进入首页,如下图: 2.点击进入"管理控制台",如下图: 3.点击"云服务器ECS&q ...
- DHCP协议格式、DHCP服务搭建、DHCP协商交互过程入门学习
相关学习资料 http://www.rfc-editor.org/rfc/rfc2131.txt http://baike.baidu.com/view/7992.htm?fromtitle=DHCP ...
- [DHCP服务]——一个验证DHCP原理实验(VMware)
大致实验拓扑图 DHCP Server端的配置 1. 安装DHCP # yum -y install dhcp 2. 拷贝配置文件 # /dhcpd.conf.sample /etc/dhcp/dhc ...
- 因客户机IP与服务器IP不在同一网段导致无盘客户机开机卡tftp,提示:PXE-E11: ARP timeout
61的地址需要在上面的地址范围段之内 问题现象] 无盘客户机启动获取DHCP后卡在tftp界面提示:PXE-E11: ARP timeout,如下图: [原因说明] 客户机的IP地址与服务器IP地址不 ...
- 服务器IP配置功能实现小结
1. 服务器网卡配置文件 /etc/sysconfig/network/ifcfg-***(eth0) linux-f1s9:/etc/sysconfig/network # cat ifcfg-et ...
- 如何修改因Informatica 8.6服务器IP而造资料库无法访问的问题
原因分析解决及如何避免(PowerCenter 8.6.1) 前几天同事安装了个PowerCenter8.6.1做测试,出去了几天回来后Administration Console无法登入了.同事用的 ...
- 如何变更站点 AD 域服务器IP地址
在 winserver 2012 单森林单域,多站点环境中,想把某一个站点AD 域服务器IP地址更改,要如何操作,才能保证客户端正常运行,不影响客户端的运行.有些朋友也经常提出类似问题. 想在不影响 ...
随机推荐
- WebUtils【MD5加密(基于MessageDigest)】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 用于MD5加密,主要场景是在调用登录接口时对密码进行MD5加密处理. 效果图 暂不需要 代码分析 基于Java.security.M ...
- 如何使用git和ssh部署本地代码到服务器
一.首先设置好自己本地的Git用户名和密码: git config --global user.name "your name" git config --global user. ...
- C#工具:MySQL忘记密码解决方法
1.进入管理员控制台停止mysql服务:net stop mysql; 2.进入mysql的安装路径,如我的安装路径为C:\Program Files\MySQL\MySQL Server 5.5,打 ...
- 微信ChatEmoji表情适配,对微信公众号开发有帮助
最近做微信公众号时发现微信ChatEmoji表情与接受的消息显示表情的问题, 微信表情后面的ChatEmoji显示不出,花了一些时间整理,把pc和手机的表情全部都整理了, 由于有两百多个显示可能有点长 ...
- git status 显示中文乱码
场景 在使用git命令行查看当前 状态时, git status 显示中文文件乱码: 解决 修改git配置, git config --global core.quotepath false
- 基于python的种子搜索网站-项目部署
本讲会对种子搜索网站的部署过程进行详细的讲解. 网站演示: https://bt.mypython.me 源码地址: https://github.com/geeeeeeeek/bt 项目部署过程 系 ...
- 学习RenderScript,以此来修改LiveWallpaper
先留个坑,花5天的时间来填满.
- 查看CPU使用率
rem 如果wmi服务(服务名为Winmgmt)坏掉了,需要到system32\webm目录下执行如下注释的命令 rem for %i in (*.dll) do RegSvr32 -s %i rem ...
- 导入虚拟机vmware,此主机支持Intel VT-x,但Intel VT-x处于禁用状态和黑屏
解决方法:进入BIOS(按什么键进入bios,需要看你用什么电脑),把Intel Virtualization Technology 设置enabled 然后是黑屏解决方法:管理员模式 ...
- Swift 产生 uuid
项目中.需要客户端生成一个唯一的识别码 let uuid = UUID().uuidString print(uuid)