最近工做須要,寫了一個Python小腳本,分享給你們,由於公司的IP用的差很少了,而後離職人員的IP有沒有及時刪除,致使沒多少IP用了,因此作了一個python腳本跑了跑,清出來一堆ping不通的IP,而後對照其餘的數據表,把可用的IP清理出來,好了,廢話很少說,上代碼:(代碼很簡單,就不作註釋了)python
輸入起始查詢IP和終止查詢IPspa
而後腳本會在這兩個IP段之間把ping通和ping不通的IP分別寫入到兩個TXT文件中code
1 # coding=utf-8 2 3 import os,time 4 import sys 5 6 start_Time=int(time.time()) 7 ip_True = open('ip_True.txt','w+') 8 ip_False = open('ip_False.txt','w+') 9 IPhost = [] 10 IPbegin = (raw_input(u'請輸入起始查詢IP: ')) 11 IPend = raw_input(u'請輸入終止查詢IP: ') 12 IP1 = IPbegin.split('.')[0] 13 IP2 = IPbegin.split('.')[1] 14 IP3 = IPbegin.split('.')[2] 15 IP4 = IPbegin.split('.')[-1] 16 IPend_last = IPend.split('.')[-1] 17 count_True,count_False = 0,0 18 for i in range(int(IP4)-1,int(IPend_last)): 19 ip = str(IP1+'.'+IP2+'.'+IP3+'.'+IP4) 20 int_IP4 = int(IP4) 21 int_IP4 += 1 22 IP4 = str(int_IP4) 23 return1=os.system('ping -n 1 -w 1 %s'%ip) 24 if return1: 25 print 'ping %s is fail'%ip 26 ip_False.write(ip+'\n') 27 count_False += 1 28 else: 29 print 'ping %s is ok'%ip 30 ip_True.write(ip+'\n') 31 count_True += 1 32 ip_True.close() 33 ip_False.close() 34 end_Time = int(time.time()) 35 print "time(秒):",end_Time - start_Time,"s" 36 print "ping通的ip數:",count_True," ping不通的ip數:",count_False