-
Notifications
You must be signed in to change notification settings - Fork 0
/
findipserver.py
executable file
·39 lines (34 loc) · 1.02 KB
/
findipserver.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
#!/usr/bin/env python
# this is the udp broadcast client
import socket, traceback
from time import sleep
from pprint import pprint
from scan import scan_interface,echo
host = '' # Bind to all interfaces
this_port = 5441 # Can be any
server_port = 2345
buffer_size = 4096
reply = bytes(1)
ints = scan_interface()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind((host, this_port))
while True:
try:
for (ifname,ip,broadcast) in ints:
echo ("sending to:",broadcast,server_port)
s.sendto(reply,(broadcast, server_port))
echo("Waiting for reply")
message, address = s.recvfrom(buffer_size)
echo("Found server:\n\t", address,":",message )
break
except (KeyboardInterrupt, SystemExit):
echo ("exiting ..")
break
except:
traceback.print_exc()
echo ("exception happened")
sleep(0.5)
s.close()
echo("finished!")