5 import xml.etree.ElementTree
as ET
6 from threading
import Thread, Event
10 print(
r'If you have more than one network interface it may happen that no scanner is found'
11 r' because the broadcast ip address does not match and the discovery packets are sent to the wrong interface.'
12 r'To fix this problem change the parameter <UDP_IP = "192.168.0.255"> '
13 r'to the broadcast address that ifconfig returns for your network interface e.g. "192.168.178.255".')
14 UDP_IP =
"192.168.0.255"
16 RANDOM_KEY=random.randrange(4294967295)
17 MESSAGE = bytes.fromhex(
'10000008ffffffffffffc8f4b6270102c0a8007effffff00')
18 MESSAGE=MESSAGE.replace(bytes.fromhex(
'c8f4b627'),RANDOM_KEY.to_bytes(4, byteorder=
'big', signed=
False))
20 DEBUGMSGENABLED =
False
23 print(
"UDP target IP: {ip}".format(ip=UDP_IP))
24 print(
"UDP target port: {port}".format(port=UDP_PORT))
26 print(
"Message: {message}".format(message=MESSAGE))
27 print(
"The scan result is available in "+str(ANSWERTIMEOUT)+
" seconds.")
46 sock = socket.socket(socket.AF_INET,
48 sock.settimeout(ANSWERTIMEOUT - 5)
49 sock.bind((HOST, UDP_PORT))
51 sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
52 sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
54 DATA, addr = sock.recvfrom(2048)
56 print(
"received message: {data}".format(data=DATA))
59 if stop_event.is_set():
61 print(
"Wait time out")
65 print(
"Wait time out 2")
68 DataList=
uniq(DataListInput)
72 print(
'Scanner found:')
73 XML_INDEX=Data.find(b
'<?xml')
75 XML_DATA=Data[XML_INDEX:]
78 root = ET.fromstring(XML_DATA)
84 DHCPClientEnabled=
"???"
85 FirmwareVersion=
"????"
87 k = child.attrib[
'key']
88 v = child.attrib[
'value']
89 if (k ==
'IPAddress'):
97 if (k==
'SerialNumber'):
99 if (k==
'DHCPClientEnabled'):
101 if (k==
'FirmwareVersion'):
103 print(
"Device type = "+DeviceType+
" SN = "+SerialNumber+
" IP = "+ipAddress+
" IPMask = "+IPMask+
" Gatway = "+IPGateway+
" DHCPEnable = "+DHCPClientEnabled+
"Firmwarevers.= "+FirmwareVersion)
107 if __name__ ==
'__main__':
109 action_thread= Thread(target=getScannerXML)
112 action_thread.start()
113 action_thread.join(timeout=ANSWERTIMEOUT+1)