Documentation for PythonAPI
22.40.00py2

example

#!/usr/bin/env python

"""
Script that creates a bunch of NetObjects, ShapingObjects and
Shapingrules for testing purposes.  Creates Broadband 30-45 with ten
random named subobjectsi Creates the same ShapingRules with the
NetObjects as host conditions.
"""

__version__ = "1.1 2008-11-14 Procera Networks"

###############################################################################
#
#                          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 sys
import random
import packetlogic2

try:
    [host, user, pwd] = sys.argv[1:]
except ValueError:
    print "Usage: %s plhost pluser plpass" % sys.argv[0]
    print "Example: %s packetlogic admin pldemo00" % sys.argv[0]
    sys.exit(1)

# Connect to PacketLogic
try:
    pl = packetlogic2.connect(host, user, pwd)
    r = pl.Ruleset()
except:
    t, v, tb = sys.exc_info()
    print "Couldn't connect to PacketLogic: %s" % v
    sys.exit(1)

if not r.shapingobject_find("unlimited"):
    r.shapingobject_add("unlimited")
shapeobj = r.shapingobject_find("unlimited")

for i in range(30, 50, 5):
    objname = "Broadband%d" % i
    if r.shapingrule_find(objname):
        r.shapingrule_remove(objname)
        r.remove("/NetObjects/%s" % objname)

    r.shapingrule_add(objname)
    a = r.shapingrule_find(objname)
    a.set_objects([shapeobj.id])
    r.add("/NetObjects/%s" % objname)

    print "adding %s" % objname
    for num in range(1, 10):
        if i == 30:
            group = "Class A"
        elif i == 35:
            group = "Class B"
        elif i == 40:
            group = "Class C"
        else:
            group = "Class D"

        netobjectname = "/NetObjects/%s/%s - %s" % (objname, group, random.randint(1000, 30000))
        r.add("%s" % netobjectname)

    all_obj = r.object_list("/NetObjects/%s" % objname)
    for obj in all_obj:
        if obj == all_obj[0]:
            a.cond_add(r.CONDITION_NETOBJECT_HOST, r.CONDITION_OP_EQ, [obj.id])
        else:
            a.cond_object_add(r.CONDITION_NETOBJECT_HOST, obj.id)

r.commit()