import time import os status1 = 'ping success' status2 = 'ping fail' def pingComputer(): for i in range(1, 256): host = '192.168.2.' + str(i) nowTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) p = os.popen("ping " + host + " -n 2") line = p.read() # print(line) if "无法访问目标主机" in line: print(nowTime, host, status2) else: print(nowTime, host, status1) """ ======================================== 主函数功能测试 ======================================== """ if __name__ == '__main__': pingComputer()
关注公众号,获取更多资料