The network is disconnected for unknown reasons in the actual project , For precise disconnection time and reason , Write a script to record the disconnection time
among
ping -c 1 192.168.1.252
-c 1 Indicates a total of one execution
another -i 5 Indicates the interval 5 Seconds to send
import os
import datetime
import time
def network_test():
count = 0
while(True):
ret = os.system('ping -c 1 192.168.1.252')
if ret == 0:
time_re = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print("OK,%s" % time_re)
#return True
else:
time_re = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
with open('./log.txt','a') as f:
f.write('time:%s\n'%time_re)
print(time_re)
count += 1
if count > 20:
break
#return False
time.sleep(2)
int2 = 10
if __name__ == '__main__':
network_test()