Documentation for PythonAPI
17.2.0py1

example

#!/usr/bin/env python
"""
Update/create bogon-netobject with list from:
http://www.cymru.com/Documents/bogon-bn-nonagg.txt
"""

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

# path to store the bogon items
objpath = '/NetObjects/Bogons'

# path to fetch the bogon list
url = 'http://www.cymru.com/Documents/bogon-bn-nonagg.txt'

# connect to the HTTP server and get the list
bogons = urllib2.urlopen(url)

# get values from command line
try:
    [plhost, pluser, plpass] = sys.argv[1:]
except:
    print "Usage: %s plhost pluser plpass" % sys.argv[0]
    sys.exit(1)

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

# find or create NetObject to store the bogon IPs
obj = r.object_get(objpath)
if obj is None:
    obj = r.object_add(objpath)

# remove old IPs
for item in obj.items:
    obj.remove(item)

# all IPs
count = 0
for line in bogons:
    line = line.strip()
    if line and line[0] != "#":
        obj.add(line.replace(" ","/"))
        count += 1

print "Note: Added %d IP's." % count

# commit the changes
r.commit()