Python3 pingIP地址,Windows和Linux

来源:互联网 发布:大数据的作用与意义 编辑:程序博客网 时间:2024/05/17 06:38

!/usr/bin/python3

Windos

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

LINUX

import oshostname ="google.com" #exampleresponse = os.system("ping " + hostname)and then check the response...if response == 0:    print hostname, 'is up!'else:    print hostname, 'is down!'