-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.py
57 lines (54 loc) · 1.54 KB
/
parse.py
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
51
52
53
54
55
56
import re
import fileinput
def parse_ping(line):
reply = re.compile('^(\d+) bytes from ([0-9\.]+): icmp_seq=(\d+) ttl=(\d+) time=(\d+\.\d+) ms')
# 64 bytes from 4.2.2.2: icmp_seq=269 ttl=57 time=33.817 ms
if '64 bytes from' in line:
match = reply.match(line)
results = match.groups()
result = {
'success': True,
'error': False,
'bytes': results[0],
'from_ip': results[1],
'icmp_seq': results[2],
'ttl': results[3],
'time': results[4],
'status': 'got-reply'
}
elif 'Request timeout for' in line:
result = {
'success': False,
'error': False,
'status': 'timeout'
}
elif 'PING ' in line:
result = {
'success': True,
'error': False,
'status': 'ping-started'
}
elif 'sendto: Network is down' in line:
result = {
'success': False,
'error': False,
'status': 'network-down'
}
elif 'sendto: No route to host' in line:
result = {
'success': False,
'error': False,
'status': 'no-route'
}
else:
result = {
'error': True,
'success': False,
'status': '[UNRECOGNIZED LINE] ' + line
}
return result
if __name__ == '__main__':
for line in fileinput.input():
rv = parse_ping(line)
if rv['error']:
print rv['status'],