剛剛接觸Python 想做點什么 聽說Python 在網絡方便很厲害 后來總結如下:
第一:發現公司都固定IP 每次新來同事都要猜一個沒有人用的IP 很費勁
第二:我們公司有的IP可以上QQ 有的不可以所以我每次也要換IP O(∩_∩)O
所以想到用Python 做一個批量Ping IP的工具 以至於方便於自 方便於人 少說多做 先上圖
原理很簡單 什么語言都可以實現的 獻丑了 上代碼
1 import subprocess 2 import string 3 import os 4 import sys 5 cmd="cmd.exe" 6 7 type = sys.getfilesystemencoding()#解決中文亂碼 8 Section = int(raw_input("請輸入正確IP段(例如:192.168.20.101,輸入 20 即可) ").decode('UTF-8').encode(type)) #客戶數據IP段數值 9 if Section>255: #校驗 10 print ("請輸入正確IP段數據!").decode('UTF-8').encode(type) 11 12 #客戶起始數據IP數值 13 begin = int(raw_input("請輸入查詢起始IP :").decode('UTF-8').encode(type)) 14 if begin>255 or begin<0: #校驗 15 print ("請輸入正確起始IP").decode('UTF-8').encode(type) 16 17 #客戶結束數據IP數值 18 end = int(raw_input("請輸入查詢結束IP :").decode('UTF-8').encode(type)) 19 if end>255 or end<0: #校驗 20 print ("請輸入正確結束IP").decode('UTF-8').encode(type) 21 22 #循環開始數據到結束IP值 23 while begin<=end: 24 return1 = os.system("ping -n 1 -w 1 192.168."+str(Section)+"."+str(begin)+"\n") #每個ip ping1次,等待時間為1s 25 if return1: 26 print ('ping %s is fail'%str(begin)) #失敗 27 else: 28 print ("ping %s is ok"%str(begin)) #成功 29 begin+=1
應該還有很多方式去操作的方式 如果你有好的方式或意見 請您留下寶貴的意見!共建中國技術 共享中國代碼 共同進步技術人生!
Demo 下載地址:http://files.cnblogs.com/yhyjy/PingIP.zip