-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduoxiancheng.py
More file actions
50 lines (40 loc) · 1.28 KB
/
duoxiancheng.py
File metadata and controls
50 lines (40 loc) · 1.28 KB
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
46
47
48
49
50
#!/usr/bin/python
# _*_coding:utf-8_*_
#
'''
名称:快速多线程ping程序
'''
import os
import pexpect
import datetime
from threading import Thread
host = ["192.168.1.1", "192.168.1.123", "192.168.2.1",
"192.168.1.1", "192.168.1.123", "192.168.2.1",
"192.168.1.1", "192.168.1.123", "192.168.2.1",
"192.168.1.1", "192.168.1.123", "192.168.2.1",
"192.168.1.1"]
report_ok = []
report_error = []
class PING(Thread):
def __init__(self, ip):
Thread.__init__(self)
self.ip = ip
def run(self):
Curtime = datetime.datetime.now()
# Scrtime = Curtime + datetime.timedelta(0,minute,0)
# print("[%s]主机[%s]" % (Curtime,self.ip))
ping = pexpect.spawn("ping -c1 %s" % (self.ip))
check = ping.expect([pexpect.TIMEOUT, "1 packets transmitted, 1 received, 0% packet loss"], 2)
if check == 0:
print("[%s] 超时 %s" % (Curtime, self.ip))
elif check == 1:
print ("[%s] %s 可达" % (Curtime, self.ip))
else:
print("[%s] 主机%s 不可达" % (Curtime, self.ip))
# 多线程同时执行
T_thread = []
for i in host:
t = PING(i)
T_thread.append(t)
for i in range(len(T_thread)):
T_thread[i].start()