Documentation for PythonAPI
22.40.00py2

example

#!/usr/bin/env python

"""
Script to modify configuration values.

Examples:

Shunt all traffic:
config_modify.py --host 192.168.1.25 --set SHUNT_ETHERTYPES 0x0000-0xffff

Restore MAX_DYNAMIC_IPS to its default value:
config_modify.py --host 192.168.1.25 --set-default MAX_DYNAMIC_IPS



"""

###############################################################################
#
#                          NO WARRANTY
#
#  BECAUSE THE PROGRAM IS PROVIDED FREE OF CHARGE, THERE IS NO WARRANTY
#  FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
#  OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
#  PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
#  OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
#  TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
#  PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
#  REPAIR OR CORRECTION.
#
#  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
#  WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
#  REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
#  INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
#  OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
#  TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
#  YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
#  PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGES.
#
###############################################################################

import packetlogic2
import optparse
import sys


#
# Parse options
#

parser = optparse.OptionParser()
grp_host = optparse.OptionGroup(parser, "PacketLogic host options", "Options determining how to connect to the PacketLogic system")
grp_host.add_option("--host", dest="host", default="192.168.1.25", action="store",
                  help="PacketLogic host to connect to")
grp_host.add_option("--user", dest="user", default="admin", action="store",
                  help="PacketLogic user to authenticate with")
grp_host.add_option("--pass", dest="passwd", default="pldemo00", action="store",
                  help="PacketLogic passwd to authenticate with")
parser.add_option_group(grp_host)

grp_act = optparse.OptionGroup(parser, "Actionsctions", "Options determining what to do")
grp_act.add_option("--set", dest="set", default=False, action="store_true",
                  help="Set a config value. Specify name and new value as arguments")
grp_act.add_option("--set-default", dest="set_default", default=False, action="store_true",
                  help="Set a config value to its default value. Specify name as argument")
grp_act.add_option("--get", dest="get", default=False, action="store_true",
                  help="Get the current value for specified config value")
grp_act.add_option("--info", dest="info", default=False, action="store_true",
                  help="Get information about specified config value")
parser.add_option_group(grp_act)

(options, args) = parser.parse_args()

#
# Validate options
#

used_actions = len([1 for x in [options.set, options.set_default, options.get, options.info] if x])

if used_actions == 0:
    parser.error("Need one action (--set, --set-default, --get or --info")

if used_actions > 1:
    parser.error("Only one action can be specified (--set, --set-default, --get or --info")

if options.get:
    if len(args) != 1:
        parser.error("--get expects exactly one argument (the name of the config value)")
elif options.set:
    if len(args) != 2:
        parser.error("--set expects exactly two arguments (the name of the config value and the new value)")
elif options.set_default:
    if len(args) != 1:
        parser.error("--set-default expects exactly one argument (the name of the config value)")
elif options.info:
    if len(args) != 1:
        parser.error("--info expects exactly one argument (the name of the config value)")

#
# Connect to PacketLogic system
#

try:
    pl = packetlogic2.connect(options.host, options.user, options.passwd)
except:
    t, v, tb = sys.exc_info()
    print "Error: Couldn't connect: %s" % v
    sys.exit(1)

#
# Get Configuration resource
#

c = pl.Config()

#
# Find requested configuration value (which happens to be first argument for all actions: args[0])
#

try:
    confval = c.get(args[0])
except ValueError:
    print "Configvalue %r not found" % args[0]
    sys.exit(1)

#
# Do requested changes
#

if options.get:
    print confval['value']
elif options.set:
    c.set(args[0], args[1])
elif options.set_default:
    c.set(args[0], confval['defvalue'])
elif options.info:
    print "%s:" % confval['key']
    print " %s" % confval['description']
    print "     value: %s" % confval['value']
    print " min value: %s" % confval['minvalue']
    print " max value: %s" % confval['maxvalue']
    print "   default: %s" % confval['defvalue']

#
# Commit changes
#

if options.set or options.set_default:
    c.commit()