最近工作需要,写了一个Python小脚本,分享给大家,因为公司的IP用的差不多了,然后离职人员的IP有没有及时删除,导致没多少IP用了,所以做了一个python脚本跑了跑,清出来一堆ping不通的IP,然后对照其他的数据表,把可用的IP清理出来,好了,废话不多说,上代码:(代码很简单,就不做注释了)
输入起始查询IP和终止查询IP
然后脚本会在这两个IP段之间把ping通和ping不通的IP分别写入到两个TXT文件中
# coding=utf-8 import os,time
import sys start_Time=int(time.time())
ip_True = open('ip_True.txt','w+')
ip_False = open('ip_False.txt','w+')
IPhost = []
IPbegin = (raw_input(u'请输入起始查询IP: '))
IPend = raw_input(u'请输入终止查询IP: ')
IP1 = IPbegin.split('.')[0]
IP2 = IPbegin.split('.')[1]
IP3 = IPbegin.split('.')[2]
IP4 = IPbegin.split('.')[-1]
IPend_last = IPend.split('.')[-1]
count_True,count_False = 0,0
for i in range(int(IP4)-1,int(IPend_last)):
ip = str(IP1+'.'+IP2+'.'+IP3+'.'+IP4)
int_IP4 = int(IP4)
int_IP4 += 1
IP4 = str(int_IP4)
return1=os.system('ping -n 1 -w 1 %s'%ip)
if return1:
print 'ping %s is fail'%ip
ip_False.write(ip+'\n')
count_False += 1
else:
print 'ping %s is ok'%ip
ip_True.write(ip+'\n')
count_True += 1
ip_True.close()
ip_False.close()
end_Time = int(time.time())
print "time(秒):",end_Time - start_Time,"s"
print "ping通的ip数:",count_True," ping不通的ip数:",count_False