-
Notifications
You must be signed in to change notification settings - Fork 1
/
PingObject.py
executable file
·43 lines (37 loc) · 1.21 KB
/
PingObject.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
"""
Python Project (Cisco Incubator 2018)
Constributors:
- Boriychuk Dima
- Gain Gaulthier
Libs used:
- https://docs.python.org/2/library/re.html
- https://docs.python.org/2/library/subprocess.html
"""
import re
import subprocess
class PingObject:
"""
Constructor
"""
def __init__(self, ip_address):
self.ip_address = ip_address
self.addresses = set()
"""
Method allowing to ping hosts via broadcast address
"""
def pingHosts(self):
try:
# Ping hosts with the broadcast address
process = subprocess.Popen("ping -c 2 -b " + str(self.ip_address), stdout = subprocess.PIPE, shell = True)
(out, err) = process.communicate()
# Check the output from the ping
for line in out.splitlines():
match = re.search(r'^\d+\s+bytes\s+from\s+(?P<IP>.*):\s+icmp_req=\d+\s+ttl=\d+\s+time=.*$', line, re.MULTILINE)
if match:
# If output matches, add address into a set
self.addresses.add(match.group('IP'))
except OSError:
# Return None if an error has occured
return None
# Return the set
return self.addresses