sick_generic_device_finder.py
Go to the documentation of this file.
00001 #!/usr/bin/env python3
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 = ''                 # Symbolic name meaning all available interfaces
00020 DEBUGMSGENABLED = False
00021 ANSWERTIMEOUT=10 #timeout in seconds
00022 # print("Test .... {<Var-ID>}".format(<VAR_ID>=<VAR|Wert>))
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 #print("Random key: {random_key}".format(random_key=RANDOM_KEY.to_bytes(4, byteorder='big', signed=False)))
00029 #inet udp communication
00030 
00031 # Event object used to send signals from one thread to another
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,  # Internet
00047                          socket.SOCK_DGRAM)  # UDP
00048     sock.settimeout(ANSWERTIMEOUT - 5)
00049     sock.bind((HOST, UDP_PORT))
00050     # broadcast rechte holen
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) # buffer size is 1024 bytes
00055         if(DEBUGMSGENABLED):
00056             print("received message: {data}".format(data=DATA))
00057         rawData.append(DATA)
00058         #DATA=b'\x90\x00\x04\xae\x00\x06w"H\x83\xc8\xf4\xb6\'\x00\x00<?xml version="1.0" encoding="UTF-8"?><NetScanResult MACAddr="00:06:77:22:48:83"><Item key="IPAddress" value="192.168.0.51" readonly="FALSE"></Item><Item key="IPMask" value="255.255.255.0" readonly="FALSE"></Item><Item key="IPGateway" value="0.0.0.0" readonly="FALSE"></Item><Item key="HostPortNo" value="2112" readonly="TRUE"></Item><Item key="HostModeClnEna" value="0" readonly="TRUE"></Item><Item key="AuxPortNo" value="2111" readonly="TRUE"></Item><Item key="DeviceType" value="TiM5xx V2.54-22.06.16" readonly="TRUE"></Item><Item key="FirmwareVersion" value="V2.54" readonly="TRUE"></Item><Item key="SerialNumber" value="16300542" readonly="TRUE"></Item><Item key="OrderNumber" value="1060445" readonly="TRUE"></Item><Item key="IPConfigDuration" value="5000" readonly="TRUE"></Item><Item key="ModeToHandleIP" value="NoExtraCmd" readonly="TRUE"></Item><Item key="LocationName" value="not defined" readonly="TRUE"></Item><Item key="HasDHCPClient" value="TRUE" readonly="TRUE"></Item><Item key="DHCPClientEnabled" value="FALSE" readonly="FALSE"></Item><Item key="IsBeepSupported" value="FALSE" readonly="TRUE"></Item><Item key="AddressingMode" value="index" readonly="TRUE"></Item></NetScanResult>'
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             #print("XML index in Answerstring is: {xml_index}".format(xml_index=XML_INDEX))
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     # We create another Thread
00109     action_thread= Thread(target=getScannerXML)
00110 
00111     # Here we start the thread and we wait 5 seconds before the code continues to execute.
00112     action_thread.start()
00113     action_thread.join(timeout=ANSWERTIMEOUT+1)
00114 
00115     # We send a signal that the other thread should stop.
00116     stop_event.set()
00117     getIpFromXmlData(rawData)
00118     sys.exit()
00119 
00120 
00121 


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Tue Jul 9 2019 05:05:34