Python Code Samples

From Lantronix Developer Wiki
Revision as of 18:11, 14 November 2014 by Vlkd2103 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Code samples

Determining the type of device

Sometimes it is useful to find out what kind of device the script is running on, so that it can be deployed on multiple types of PremierWave devices without modifying.

The code below creates the pwDevice class. When an instance of this class is created, it will first find out what the MAC address of the device is. This can be useful to use as a unique identifier. It will then determine which type of device it is. As an example, it also creates a property called import_file which is a string with the path of the file used for importing configuration. This will become useful if a program needs to change configuration of the device.

import os.path
class pwDevice:
	def __init__(self):
		mac = commands.getoutput("ifconfig eth0| grep HWaddr | awk '{ print $5 }'").strip()			
		self.mac = mac.translate(None,':')
		if os.path.exists('/ltrx_user/pwxcr/pwxc_hspa_import.xcr'):
			self.type = "PremierWave XC HSPA+"
			self.import_file = '/ltrx_user/pwxcr/pwxc_hspa_import.xcr'
		elif os.path.exists('/ltrx_user/pwxcr/pwxn_import.xcr'):
			self.type = "PremierWave XN"
			self.import_file = '/ltrx_user/pwxcr/pwxn_import.xcr'
		elif os.path.exists('/ltrx_user/pwxcr/pwen_import.xcr'):
			self.type = "PremierWave EN"
			self.import_file = '/ltrx_user/pwxcr/pwen_import.xcr'
		elif os.path.exists('/ltrx_user/pwxcr/pwse1000_import.xcr'):
			self.type = "PremierWave SE1000"
			self.import_file = '/ltrx_user/pwxcr/pwse1000_import.xcr'
		else:
			self.type = "Unknown"

This example will create an instance, and log the MAC address to a file in the PremierWave's filesystem:

import logging
logging.basicConfig(level=logging.DEBUG, filename='pythonDebug.log')
if __name__ == '__main__':
	try:
		dev = pwDevice()
		logging.debug(dev.mac)
	except Exception, ex:
		logging.exception('Was not able to log')

Rebooting

The Lantronix application running on the PremierWave device safely handles the reboot cycle. You can kick off a reboot by importing XML into the configuration file that indicates to the application to start a reboot.

xml = '''<?xml version="1.0" standalone="yes"?>
<!DOCTYPE configrecord [  <!ELEMENTconfigrecord (configgroup+)>   
<!ELEMENT configgroup (configitem+,configgroup*)>   
<!ELEMENT configitem(value+)>   
<!ELEMENT value (#PCDATA)>   
<!ATTLIST configrecord version CDATA #IMPLIED>   
<!ATTLISTconfiggroup name CDATA #IMPLIED>   
<!ATTLIST configgroup instance CDATA #IMPLIED>   
<!ATTLIST configitem nameCDATA #IMPLIED>   
<!ATTLIST configitem instance CDATA #IMPLIED>   
<!ATTLIST value name CDATA #IMPLIED>]>
<configrecord version = "0.1.0.0T0">
<configgroup name = "xml import control">       
<configitem name = "reboot">           
<value>enable</value>       
</configitem>   
</configgroup>
</configrecord>'''

import logging
logging.basicConfig(level=logging.DEBUG, filename='pythonDebug.log')
if __name__ == '__main__':
       dev = pwDevice()	
       try: 
	       f = open(dev.import_file,'w')
	       f.write(xml)
	       f.close()
       except Exception, ex:
	       logging.exception('Reboot')
Personal tools
Namespaces

Variants
Actions
Navigation
Tools