Windows 中通过Python实现ping命令加时间戳

            由于ping命令在ping的时候无法加入时间,不能够直观分析结果。便想在ping的时候加入时间戳。

1.首先需要系统配置了Python的环境,我的环境如下,这里就不介绍环境搭建步骤。
Windows 中通过Python实现ping命令加时间戳

2.以下是代码展示,新建一个ping.py文件
import os
import re
import datetime

file = open('d:\ping_log.txt','a') #以可写打开目标文件
for a in range(1,10):
ping = os.popen('ping 192.168.X.X -n 1').read() #执行1次ping命令
ping = re.compile(r'来自..+',re.M).findall(ping) #通过正则表达式筛选出需要的哪一行
nowTime=datetime.datetime.now() #获取当前系统的时间戳
file.write(str(ping)+' ,'+str(nowTime)+'\n') #写入内容

file.close() #关闭文件流

3.打开新生成的文件就可以查看了。Windows 中通过Python实现ping命令加时间戳

猜你喜欢

转载自blog.51cto.com/binxing/2123141