python测试IP地址是否ping通
import time
import os
def pingComputer():
for i in range(1, 256):
host = '192.168.2.' + str(i)
status1 = 'ping success'
status2 = 'ping faild'
nowTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
p = os.popen("ping "+ host + " -n 2")
line = p.read()
if "无法访问目标主机" in line:
print(nowTime, host, status2)
else:
print(nowTime, host, status1)