import os
import sys
import time
import subprocess
cur_path = r'C:\Users\xxx\Desktop\201_monitor' # save log.txt path
hostname = '10.2.53.201' # ip address
def pyping(ipaddress):
cur_time = time.strftime('%Y_%m_%d-%H:%M:%S',time.localtime(time.time()))
proc = subprocess.Popen(
['ping', '-n', '1', ipaddress],
stdout=subprocess.PIPE)
stdout, stderr = proc.communicate()
ping_ret ='T:%s, R:%s' % (cur_time, stdout.decode("gbk").split("\n")[2])
print(ping_ret)
return ping_ret
log_path = cur_path + "\\%s_ping_log.txt" % hostname
with open(log_path, "a") as logfile:
while 1:
ret_ping = pyping(hostname)
logfile.write(ret_ping+'\r\n')
logfile.flush()
time.sleep(60) # a print every a minute
改了一下别人的,原网址在python3上有点问题