2015-08-07 12:42:59 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import socket
|
|
|
|
import zlib
|
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
|
2015-08-08 12:49:52 +02:00
|
|
|
|
|
|
|
def request(request, targets, interface, timeout=0.5, singleshot=False):
|
2015-08-07 12:42:59 +02:00
|
|
|
try:
|
2015-08-08 12:49:52 +02:00
|
|
|
if_id = socket.if_nametoindex(interface)
|
2015-08-07 12:42:59 +02:00
|
|
|
except OSError:
|
2015-08-08 12:49:52 +02:00
|
|
|
print('interface \'{}\' not found'.format(ifname), file=sys.stderr)
|
2015-08-07 12:42:59 +02:00
|
|
|
return []
|
|
|
|
|
|
|
|
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
|
|
|
|
|
|
|
|
# request
|
|
|
|
message = bytes('GET {}'.format(request), 'utf-8')
|
2015-08-08 12:49:52 +02:00
|
|
|
|
|
|
|
for target in targets:
|
|
|
|
sock.sendto(message, (target, 1001, 0, if_id))
|
2015-08-07 12:42:59 +02:00
|
|
|
|
|
|
|
print('+ {:s}'.format(str(message)), file=sys.stderr)
|
|
|
|
|
|
|
|
sock.settimeout(timeout)
|
|
|
|
|
|
|
|
# receive
|
|
|
|
responses = {}
|
|
|
|
rsp, err = 0, 0
|
|
|
|
while True:
|
|
|
|
print('\r+ {rsp} responses, {err} errors'.format(**locals()), end='', file=sys.stderr)
|
|
|
|
|
|
|
|
try:
|
|
|
|
buffer, address = sock.recvfrom(2048)
|
|
|
|
except socket.timeout:
|
|
|
|
print('\n+ no replies for %f seconds, continuing...' % timeout, file=sys.stderr)
|
|
|
|
break
|
|
|
|
|
|
|
|
try:
|
|
|
|
source = address[0].split('%')[0]
|
|
|
|
data = zlib.decompress(buffer, -15)
|
|
|
|
nodeinfo = json.loads(data.decode('utf-8'))
|
|
|
|
responses[source] = nodeinfo
|
|
|
|
rsp += 1
|
|
|
|
except (zlib.error, UnicodeDecodeError, ValueError):
|
|
|
|
err += 1
|
|
|
|
print('- unreadable answer from {addr}'.format(addr=source), file=sys.stderr)
|
|
|
|
|
|
|
|
if singleshot:
|
|
|
|
break
|
|
|
|
|
|
|
|
return responses.values()
|
|
|
|
|