-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_servers.py
More file actions
44 lines (31 loc) · 1.01 KB
/
find_servers.py
File metadata and controls
44 lines (31 loc) · 1.01 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
import socket
from os import popen
from re import search
class SearchServer:
socket.setdefaulttimeout(0.1)
# ""
@staticmethod
def connect(hostname, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
res = sock.connect_ex((hostname, port))
if res == 0:
sock.settimeout(None)
return sock
else: return False
def look_for_server(self, port, recent_servers=None):
ips = []
ips += recent_servers
for device in popen('arp -a'):
ip = search(r'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b', device)
if ip:
ip_found = ip.group(0)
ips.append(ip_found)
for ip in ips:
res = self.connect(ip, port)
if res: return res, ip
return 1, 1
# ""
if __name__ == "__main__":
ipFound = SearchServer().look_for_server(3737)
if ipFound != 1:
print("Device found at : ", ipFound)