python3 使用代码 ping 服务器
import subprocess as sp
url = 'www.baidu.com'
status, result = sp.getstatusoutput("ping " + url + " -w 2000")
print(status)
print(result)
if "请求超时" in result:
print('hah')
!/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!'