-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_ping程序
45 lines (43 loc) · 1.65 KB
/
python_ping程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/python3
#######################################################
# This program is to check the ping in the Live Room #
# Date: 2020-4-21 #
# Author: cuijianzhe #
# Email: [email protected] #
#######################################################
import threading
import os
import subprocess
import re
import datetime as dt
import time
def ping_host(ip):
nowdate = dt.datetime.now().strftime('%Y%m%d') # 获取文件日期后缀
p = subprocess.Popen(["ping.exe", '-n', '1', '-w', '1', ip],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
out = p.stdout.read().decode('gbk')
ping = re.search(r'来自..+|请求..+', out).group()
now_time = dt.datetime.now().strftime('%F %T')
file_name = 'ping%s_%s.txt' % (ip,nowdate)
with open('C:\limi_ppt_log\%s' % file_name, 'a') as f:
f.write(str(now_time) + ' ' + str(ping))
if __name__ == '__main__':
hosts = ['www.baidu.com', '10.200.200.1', 'api-teacher.limiketang.com', 'www.qq.com']
path = "C:\limi_ppt_log"
if os.path.isdir(path):
while True:
time.sleep(1)
for IP in hosts:
#多线程同时执行
thread = threading.Thread(target=ping_host,args=(IP,))
thread.start()
else:
os.mkdir(path)
while True:
time.sleep(1)
for IP in hosts:
thread = threading.Thread(target=ping_host, args=(IP,))
thread.start()