Go to the documentation of this file.00001
00002
00003 import socket
00004 import random
00005 import xml.etree.ElementTree as ET
00006 from threading import Thread, Event
00007 import sys
00008 sys.stderr = object
00009
00010 print(r'If you have more than one network interface it may happen that no scanner is found'
00011 r' because the broadcast ip address does not match and the discovery packets are sent to the wrong interface.'
00012 r'To fix this problem change the parameter <UDP_IP = "192.168.0.255"> '
00013 r'to the broadcast address that ifconfig returns for your network interface e.g. "192.168.178.255".')
00014 UDP_IP = "192.168.0.255"
00015 UDP_PORT = 30718
00016 RANDOM_KEY=random.randrange(4294967295)
00017 MESSAGE = bytes.fromhex('10000008ffffffffffffc8f4b6270102c0a8007effffff00')
00018 MESSAGE=MESSAGE.replace(bytes.fromhex('c8f4b627'),RANDOM_KEY.to_bytes(4, byteorder='big', signed=False))
00019 HOST = ''
00020 DEBUGMSGENABLED = False
00021 ANSWERTIMEOUT=10
00022
00023 print("UDP target IP: {ip}".format(ip=UDP_IP))
00024 print("UDP target port: {port}".format(port=UDP_PORT))
00025 if(DEBUGMSGENABLED):
00026 print("Message: {message}".format(message=MESSAGE))
00027 print("The scan result is available in "+str(ANSWERTIMEOUT)+" seconds.")
00028
00029
00030
00031
00032 stop_event = Event()
00033
00034
00035 rawData =[]
00036
00037 def uniq(input):
00038 output = []
00039 for x in input:
00040 if x not in output:
00041 output.append(x)
00042 return output
00043
00044 def getScannerXML():
00045 global rawData
00046 sock = socket.socket(socket.AF_INET,
00047 socket.SOCK_DGRAM)
00048 sock.settimeout(ANSWERTIMEOUT - 5)
00049 sock.bind((HOST, UDP_PORT))
00050
00051 sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
00052 sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
00053 while True:
00054 DATA, addr = sock.recvfrom(2048)
00055 if(DEBUGMSGENABLED):
00056 print("received message: {data}".format(data=DATA))
00057 rawData.append(DATA)
00058
00059 if stop_event.is_set():
00060 sock.close()
00061 print("Wait time out")
00062 if (DEBUGMSGENABLED):
00063 print(rawData)
00064 break
00065 print("Wait time out 2")
00066
00067 def getIpFromXmlData(DataListInput):
00068 DataList=uniq(DataListInput)
00069 for Data in DataList:
00070 if b"<?xml" in Data:
00071 if (DEBUGMSGENABLED):
00072 print('Scanner found:')
00073 XML_INDEX=Data.find(b'<?xml')
00074
00075 XML_DATA=Data[XML_INDEX:]
00076 if (DEBUGMSGENABLED):
00077 print(XML_DATA)
00078 root = ET.fromstring(XML_DATA)
00079 ipAddress = "???"
00080 IPMask= "???"
00081 IPGateway = "???"
00082 DeviceType = "???"
00083 SerialNumber = "???"
00084 DHCPClientEnabled= "???"
00085 FirmwareVersion= "????"
00086 for child in root:
00087 k = child.attrib['key']
00088 v = child.attrib['value']
00089 if (k == 'IPAddress'):
00090 ipAddress=v
00091 if (k == 'IPMask'):
00092 IPMask=v
00093 if (k== 'IPGateway'):
00094 IPGateway=v
00095 if (k=='DeviceType'):
00096 DeviceType=v
00097 if (k=='SerialNumber'):
00098 SerialNumber=v
00099 if (k=='DHCPClientEnabled'):
00100 DHCPClientEnabled=v
00101 if (k=='FirmwareVersion'):
00102 FirmwareVersion=v
00103 print("Device type = "+DeviceType+" SN = "+SerialNumber+" IP = "+ipAddress+" IPMask = "+IPMask+" Gatway = "+IPGateway+" DHCPEnable = "+DHCPClientEnabled+"Firmwarevers.= "+FirmwareVersion)
00104
00105
00106
00107 if __name__ == '__main__':
00108
00109 action_thread= Thread(target=getScannerXML)
00110
00111
00112 action_thread.start()
00113 action_thread.join(timeout=ANSWERTIMEOUT+1)
00114
00115
00116 stop_event.set()
00117 getIpFromXmlData(rawData)
00118 sys.exit()
00119
00120
00121