Note: pycontrol v1 is deprecated. A new article and script have been added to work through the bigsuds library.

Last time out I built the foundation of a route management application which grabs the routing table from the BIG-IP utilizing the python implementation of iControl called pyControl.  This time, we'll extend the application with a route deletion function.

New iControl SDK Method --> delete_static_route

This method takes one parameter, routes, in the form of a dictionary with the keys specified as destination and netmask.  Adding routes takes a little extra effort, but that's all we need to delete a route.

New CLI Methods

As this effort is expanding, I saw the need to not just accept arguments, but to offer options as well.  For example, you may just want to view the route table, Or you may want to delete or add a route.  That becomes cumbersome on trying to remember where your arguments go, and counting arguments, etc, for the different use cases.  So I switch from system arguments to the optparse module.  It's pretty simple, specify the flags and what they do, how to reference them, etc.  This code is basic:

        from optparse import OptionParser
        parser = OptionParser()
       
        parser.add_option("-d",  "--device",  action="store", type="string",  dest="host")
        parser.add_option("-u", "--username",  action="store", type="string",  dest="uname")
        parser.add_option("-r",  "--delRoute",  action="store",  type="string",  dest="delrouteFileName")
        parser.add_option("-a",  "--addRoute",  action="store",  type="string",  dest="addrouteFileName")
        (options, args) = parser.parse_args()

        if options.delrouteFileName:
            del_tmmRoutes(rt,  options.delrouteFileName)

 

-h seemed natural for hostname, but it is builtin as help, so I couldn't touch that one.  So I used -d for device, but I also wanted -d for delete, but -r for remove works just as well.

Specifying the -r flag expects a file name, which in my example I aptly called routes.txt.  Format for the file should have a header row with destination and netmask, then the actual routes you want deleted:

destination,netmask
172.16.1.0,255.255.255.0
172.16.2.0,255.255.255.0
172.16.10.0,255.255.255.0

Now that I have the externals in place, I need to create the del_tmmRoutes function to pull that data in from the file, format it, then delete the routes.  This is accomplished in this function:

def del_tmmRoutes(obj,  filename):
   
    routefile = open(filename,  'r')
    headers = routefile.readline().strip().split(',')
    stored_rows = []
   
    for line in routefile:
        route = line.strip().split(',')
        stored_rows.append(dict(zip(headers,  route)))


    for route in stored_rows:
        obj.delete_static_route(routes = [route])
        print "Deleting Route ",  route

 

 When run from the command line, I can see that the rejected routes in my routing table are now deleted:

C:\dev\pyScripts>python ltmRouteManager_new.py -u admin -d 10.10.20.5 -r routes.txt
admin, enter your Password:
 Loading WSDL: Networking.RouteTable.wsdl
Deleting Route  {'netmask': '255.255.255.0', 'destination': '172.16.1.0'}
Deleting Route  {'netmask': '255.255.255.0', 'destination': '172.16.2.0'}
Deleting Route  {'netmask': '255.255.255.0', 'destination': '172.16.10.0'}



TMM IP Routes: (net mask ip)
        0.0.0.0 0.0.0.0 10.10.20.1



TMM Pool Routes: (net mask pool)



TMM Vlan Routes: (net mask vlan)
        172.17.1.0 255.255.255.0 mgmt_tools
        172.17.2.0 255.255.255.0 mgmt_tools
        172.17.10.0 255.255.255.0 mgmt_tools



TMM Rejected Routes: (net mask)

 

The wiki entry for the pyControl route management script has been updated to reflect these changes.