Difference between revisions of "Lantronix Discovery Protocol"

From Lantronix Wiki!
Jump to navigation Jump to search
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This example is a Python app in source code that shows how to search and find Lantronix Devices using a UDP broadcast. It is run from the command line.  
+
This example is a Python application in source code that shows how to search and find Lantronix Devices. It is run from the command line. <br>
<br>
+
The Lantronix Discovery protocol uses a UDP broadcast out port 77FE on a network interface.  All Lantronix devices on that subnet will respond with 30 bytes of data.<br>
 +
Their MAC address is included in the 30 bytes <br>
  
 
=== Prerequisites ===
 
=== Prerequisites ===
1) import socket, time and sys
+
1) import socket, time and sys <br>
2) While this code could be incorporated in a Premierwave device, it was written and tested on a PC. (win 7)  
+
2) While this code could be incorporated and run from a PremierWave device, it was written and tested on a PC. (win 7) <br>
  
 
=== Sample Code ===
 
=== Sample Code ===
 
  <nowiki>
 
  <nowiki>
 
# utility to search and find Lantronix devices
 
# utility to search and find Lantronix devices
#
 
# written by:
 
# Gary Marrs
 
# Lantronix FAE
 
# garym@lantronix.com
 
# 03-20-15
 
# Rev 0.1
 
#
 
  
 
import socket  #for sockets
 
import socket  #for sockets
Line 22: Line 15:
 
import time
 
import time
  
j = 0
+
# Replace this with the IP address of your network interface
k = 0
+
# Or write a function that programmatically finds your IP address
host = ''
+
HOST = '192.168.1.100'
port = 30718
+
PORT = 30718
 
DATA = []
 
DATA = []
 
LTRXnode = []
 
LTRXnode = []
packet = []
 
 
   
 
   
 
print "searching for Lantronix devices...\n"
 
print "searching for Lantronix devices...\n"
Line 37: Line 29:
 
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
 
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind((host,port))
+
s.bind((HOST,PORT))
 
s.settimeout(2)
 
s.settimeout(2)
 
except socket.error:
 
except socket.error:
Line 48: Line 40:
  
 
try:
 
try:
s.sendto(msg, ("192.168.1.255", port))
+
s.sendto(msg, ('<broadcast>', PORT))
 
except socket.error, msg:
 
except socket.error, msg:
 
print 'Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
 
print 'Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
 
sys.exit()
 
sys.exit()
#time.sleep(4)
+
 
 
t1 = 0
 
t1 = 0
 
t0 = time.clock() # set start time
 
t0 = time.clock() # set start time
Line 64: Line 56:
 
LTRXnode.append(address[0])
 
LTRXnode.append(address[0])
 
DATA.append(packet[48:60])
 
DATA.append(packet[48:60])
j += 1
 
 
except Exception:
 
except Exception:
#print "no rcv"
 
 
s.close()
 
s.close()
 
 
 
time.sleep(1)
 
time.sleep(1)
 
t1 = time.clock() - t0 # find difference in time
 
t1 = time.clock() - t0 # find difference in time
#print t1
 
  
 
# print out results of search
 
# print out results of search
print "%d devices found " % j
+
print "%d devices found " % len(LTRXnode)
 
 
for x in LTRXnode:
+
for idx, node in enumerate(LTRXnode):
print "%s  %s " % (x,DATA[k])
+
print "%s  %s " % (node,DATA[idx])
k +=1
 
  
 
print "\n"
 
print "\n"
 
 
raw_input('Press the Enter Key to Exit\n')</nowiki>
+
raw_input('Press the Enter Key to Exit\n')
 +
</nowiki>

Latest revision as of 17:10, 27 March 2015

This example is a Python application in source code that shows how to search and find Lantronix Devices. It is run from the command line.
The Lantronix Discovery protocol uses a UDP broadcast out port 77FE on a network interface. All Lantronix devices on that subnet will respond with 30 bytes of data.
Their MAC address is included in the 30 bytes

Prerequisites

1) import socket, time and sys
2) While this code could be incorporated and run from a PremierWave device, it was written and tested on a PC. (win 7)

Sample Code

# utility to search and find Lantronix devices

import socket   #for sockets
import sys  	#for exit
import time

# Replace this with the IP address of your network interface
# Or write a function that programmatically finds your IP address
HOST = '192.168.1.100'	
PORT = 30718
DATA = []
LTRXnode = []
 
print "searching for Lantronix devices...\n"
 
# create datagram udp socket
try:
	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,PORT))
	s.settimeout(2)
except socket.error:
	print 'Failed to create socket'
	sys.exit()
 

#Set the search string
msg = chr(0)+chr(0)+chr(0)+chr(0xf6)

try:
	s.sendto(msg, ('<broadcast>', PORT))
except socket.error, msg:
	print 'Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
	sys.exit()

t1 = 0
t0 = time.clock()										# set start time
	
while (t1<3):											# loop for no more than 3 seconds
	try:
		# receive data from client (data, addr)
		d, address = s.recvfrom(128)
		packet = "".join("%02x" % ord(c) for c in d)
		if packet[6:8] == "f7":
			LTRXnode.append(address[0])
			DATA.append(packet[48:60])
	except Exception:
		s.close()
	
	time.sleep(1)
	t1 = time.clock() - t0								# find difference in time

# print out results of search
print "%d devices found " % len(LTRXnode)
	
for idx, node in enumerate(LTRXnode):
	print "%s  %s " % (node,DATA[idx])

print "\n"
		
raw_input('Press the Enter Key to Exit\n')