Get the App
SLTechnology News&Howtos  ›  Servers  › 

Implementation of ping command plus timestamp through Python in Windows

Shulou Source: shulou.com Published: 2022-06-03 02:47:00 09月22日 Update

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.

Tags: File time environment command time system intuitive one line code content new regular step goal result expression selection analysis configuration Zhongtong Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Technology Docker Shulou Information vpn