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)
posted @ 2020-11-23 10:00  lijifei  阅读(3587)  评论(0编辑  收藏  举报