您的位置:首页 > 运维架构 > Linux

Python3 pingIP地址,Windows和Linux

2017-09-14 18:13 459 查看

!/usr/bin/python3

Windos

import subprocess as sp
ip =['10.168.10.161','10.168.10.209','10.168.10.54','10.168.10.55','10.168.10.123']
good = 0
bad = 0
count = 0
badIp=[]
for url in ip:
status, result = sp.getstatusoutput("ping " + url + " -w 2000")
count+=1
if "请求超时" in result:
bad+=1
badIp.append(url)
else:
good +=1
print(badIp)
print('总共ping了 %d条ip'%(count))
print('其中%d条有问题'%(bad))
print('%d条没问题'%(good))
print('坏ip是:')
print(badIp)


LINUX

import os
hostname ="google.com" #example
response = os.system("ping " + hostname)

and then check the response...
if response == 0:
print hostname, 'is up!'
else:
print hostname, 'is down!'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python