Implementation of ping command plus timestamp through Python in Windows
Because the ping command cannot add time to ping, the result cannot be analyzed visually. I want to add a timestamp to ping.
1. First of all, the system needs to configure the environment of Python, my environment is as follows, I will not introduce the steps of setting up the environment here.
two。 The following is a code demonstration to create a new ping.py file
Import os
Import re
Import datetime
File = open ('d:\ ping_log.txt','a') # Open the target file in writable form
For an in range (1d10):
Ping = os.popen ('ping 192.168.X.X-n 1'). Read () # execute the ping command once
Ping = re.compile (r 'from.. +', re.M) .findall (ping) # filter out which line you need through regular expressions
NowTime=datetime.datetime.now () # get the timestamp of the current system
File.write (str (ping) +','+ str (nowTime) +'\ n') # write content
File.close () # closes file stream
3. Open the newly generated file and you can view it.