Announcement

Collapse
No announcement yet.

Changing configuration from script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Changing configuration from script

    Sometimes you might want to change configuration from a running script. This example shows how to turn off a tunnel. If you put this into a file named tunnelOff.py in the PremierWave filesystem, you would call it by running /usr/bin/python tunnelOff.py 1 (or 2, to turn off tunnel 2).

    You can check out the PremierWave's Command Reference document to see the values that you can modify via XML.

    Note that the WriteConfig function checks what type of PremierWave this is by looking for the config file. If you know that you're always on a EN, or always on an XN, then just define the config variable to that. No reason to do expensive file existence checks.

    Code:
    #!/usr/bin/env python
    
    def WriteConfig(xml):
    	import os.path
    	
    	config = '/ltrx_user/pwxcr/pwxn_import.xcr'
    	if not os.path.exists(config):
    		config = '/ltrx_user/pwxcr/pwen_import.xcr'
    		if not os.path.exists(config):
    			return 'Cannot determine what kind of PremierWave I am'
    	
    	try:
    		f = open(config, 'w')
    		f.write(xml)
    		f.close()
    	except Exception:
    		f.close()
    		return 'Failed to configure device'
    	return 'Success!'
    
    def TunnelOff(tunnel):
    	xml = '<?xml version="1.0" standalone="yes"?>\n<configrecord version = "0.1.0.0T0">'
    	xml = xml + '<configgroup name = "tunnel accept" instance = "'+tunnel+'">\n<configitem name = "accept mode">\n<value>Disable</value>\n'
    	xml = xml + '</configitem>\n</configgroup>\n'
    	xml = xml + '</configrecord>'
    	
    	return xml
    	
    	
    def main():
    	import argparse
    	parser = argparse.ArgumentParser()
    	parser.add_argument("tunnel", choices=['1','2'], help="Tunnel to turn off")
    	args = parser.parse_args()
    	
    	WriteConfig(TunnelOff(args.tunnel))
    	
    
    
    if __name__ == "__main__":
    	main()
Working...
X