您的位置:首页 > 编程语言 > Python开发

Python ping ip

2015-11-12 09:55 585 查看
代码:

import subprocess

import string

import os

import sys

cmd="cmd.exe"

type = sys.getfilesystemencoding()#save code error

Section = int(raw_input("please input right IP(example:135.251.148.151,such as input 148) ").decode('UTF-8').encode(type)) #客户数据IP段数值

if Section>255: #check IP

print ("please input right IP!").decode('UTF-8').encode(type)

#client start IP

begin = int(raw_input("please input start IP :").decode('UTF-8').encode(type))

if begin>255 or begin<0: #check IP

print ("please input right start IP").decode('UTF-8').encode(type)

#client end IP

end = int(raw_input("please input end IP :").decode('UTF-8').encode(type))

if end>255 or end<0: #check IP

print ("please input right IP").decode('UTF-8').encode(type)

#start to ping from start ip to end ip

while begin<=end:

return1 = os.system("ping -n 4 -w 1 135.251."+str(Section)+"."+str(begin)+"\n") #every ip ping 4 times, wait time 1s

if return1:

print ('ping %s is fail'%str(begin)) #failed

else:

print ("ping %s is ok"%str(begin)) #success

begin+=1

运行结果:

please input right IP(example:135.251.148.151,such as input 148) 148

please input start IP :150

please input end IP :151

ping 150 is fail

ping 151 is ok
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: