Documentation for PythonAPI
22.40.00py2

example

#!/usr/bin/env python

"""
Script to import subscribers to the PacketLogic ruleset. The input
should be a commaseparated file with three fields: "shaping class, ip,
netobject name". A shaping rule and a shaping object is added for each
shaping class. The shaping rule will have a host netobject condition
with the netobjects belonging to that class.

Example:

If the input file contains:

rule ,IP                ,NetObject
Small,10.0.10.20,Customer 12345
Small,10.0.10.25,Customer 23456
Large,10.0.10.28,Customer 34567

Three netobjects will be created:
  "Customer 12345" with 10.0.10.20 as item
  "Customer 23456" with 10.0.10.25 as item
  "Customer 34567" with 10.0.10.28 as item
Two shaping objects will be created: "Small" and "Large",
  They will not have any limits specified, that needs
  to be done manually.
Two shaping rules will be created: "Small" and "Large".
  "Small" will use the shaping object "Small" and
  have a "Host Netobject" condition with objects
  "Customer 12345" and "Customer 23456"
  "Large" will use the shaping object "Large" and
  have a "Host Netobject" condition with object
  "Customer 34567"
"""

__version__ = "1.2 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 packetlogic2

# Handle arguments
try:
    [plhost, pluser, plpass, prefix, filename] = sys.argv[1:]
except ValueError:
    print "Usage: %s plhost pluser plpass prefix csvfile" % sys.argv[0]
    print "Example: %s 192.168.1.25 admin pldemo00 / users.csv" % sys.argv[0]
    sys.exit(1)

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

# Make sure we get sane path
if prefix[0] != '/':
    prefix = '/' + prefix
if prefix[-1] != '/':
    prefix = prefix + '/'

# Make sure the parent object exists
if prefix != '/':
    no = r.object_get("/NetObjects%s" % prefix[:-1])
    if not no:
        no = r.object_add("/NetObjects%s" % prefix[:-1])

# Open file
if filename == "-":
    f = sys.stdin
else:
    try:
        f = file(filename)
    except:
        t, v, tb = sys.exc_info()
        print "Couldn't open file: %s" % v
        sys.exit(1)

for lineno, line in enumerate(f):
    lineno += 1
    line = line.rstrip()
    try:
        [cls, ip, name] = line.split(",", 2)
        name = name.decode("utf-8")
    except:
        print "Couldn't parse line %d, ignoring" % lineno
        continue

    # check if there already is a shaping rule for the class
    sr = r.shapingrule_find(cls)
    if not sr:
        # otherwise create it
        so = r.shapingobject_add(cls, split=r.SPLIT_NONE)
        sr = r.shapingrule_add(cls)
        sr.set_objects([so.id])
        print ("Added shaping rule and object '%s', " +
               "you need to setup limits manually") % cls

    # check if there already is a netobject
    no = r.object_get("/NetObjects%s%s" % (prefix, name.encode("utf-8")))
    if not no:
        # otherwise create it
        r.object_add("/NetObjects%s%s" % (prefix, name.encode("utf-8")))
        no = r.object_get("/NetObjects%s%s" % (prefix, name.encode("utf-8")))

    # add ip to netobject unless it already is in there
    if ip not in no.items:
        no.add(ip)

    # add netobject to shaping rule.
    if not sr.conditions.has_key(r.CONDITION_NETOBJECT_HOST):
        sr.cond_add(r.CONDITION_NETOBJECT_HOST, r.CONDITION_OP_EQ, [no.id])
    elif no.id not in sr.conditions[r.CONDITION_NETOBJECT_HOST].objects:
        sr.cond_object_add(r.CONDITION_NETOBJECT_HOST, no.id)

r.commit()