Documentation for PythonAPI
22.40.00py2

example

#!/usr/bin/env python

"""
Script to remove a dynamic IP address from a specific Object, with retries
if an exception occurs, like 'Dynamic netobjects not available now'.
"""

__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 time
import packetlogic2

class Pl:
    def __init__(self, pladdr, pluser, plpass):
        pl = packetlogic2.connect(pladdr, pluser, plpass)
        self.ruleset = pl.Ruleset()
        self.realtime = pl.Realtime()
  
    def _retry(self, func, *args):
        for retry in range(30):
            try:
                ret = func(*args)
                break

            except:
                time.sleep(0.3)
                continue

        else:
            ret = func(*args)

        return ret

    def _dyn_add(self, id, ip):
        if ip and not (id, ip) in self.realtime.dyn_list():
            self.realtime.dyn_add(id, ip)

    def dyn_add(self, id, ip):
        """ Add dynamic ip to netobject, if not present."""
        return self._retry(self._dyn_add, id, ip)
    
    def _dyn_remove(self, id, ip):
        if ip and (id, ip) in self.realtime.dyn_list():
            self.realtime.dyn_remove(id, ip)

    def dyn_remove(self, id, ip):
        """ Remove dynamic ip from netobject, if present."""
        return self._retry(self._dyn_remove, id, ip)

    def dyn_list(self):
        """ List dynamic netobject items."""
        return self._retry(self.realtime.dyn_list)


try:
    pladdr, pluser, plpass, objpath, item = sys.argv[1:]
except:
    print "Usage: %s PLADDRESS USER PASSWORD OBJECTPATH ITEM" % sys.argv[0]
    print "Example:"
    print " %s 192.168.1.7 brian Passw0rd /NetObjects/Customers/C10 192.168.1.251" % sys.argv[0]
    sys.exit(1)
      
# try create a class that connects to the PacketLogic system, or exit program
try:
    pl = Pl(pladdr, pluser, plpass)
except:
    t, v, tb = sys.exc_info()
    print "Error: Couldn't connect: %s" % v
    sys.exit(1)

# find the ibject
obj = pl.ruleset.object_get(objpath)
if obj is None:
    print "could not find %s" % objpath
else:
    pl.dyn_remove(obj.id, item)