cancel
Showing results for 
Search instead for 
Did you mean: 
Login & Join the DevCentral Connects Group to watch the Recorded LiveStream (May 12) on Basic iControl Security - show notes included.

Problem this snippet solves:

This script prints out the static TMM routing table from BIG-IP and allows optional route adds/delete from text files.

How to use this snippet:

usage: getRoutes.py [-h] -s SYSTEM -u USERNAME [-d DELROUTE] [-a ADDROUTE]

For deletes, route_deletes.txt should be formatted like:

destination,netmask
172.16.1.0,255.255.255.0
172.16.2.0,255.255.255.0
172.16.3.0,255.255.255.0
172.16.4.0,255.255.255.0

For adds, route_adds.txt should be formatted like:

name,address,netmask,route_type,gateway
/Common/r5,172.16.1.0,255.255.255.0,pool,/Common/testpool
/Common/r6,172.16.2.0,255.255.255.0,vlan,/Common/vmnet3
/Common/r7,172.16.3.0,255.255.255.0,gateway,10.10.10.1
/Common/r8,172.16.4.0,255.255.255.0,gateway,10.10.10.1

Script

getBIGIProutes.py

Code :

__author__ = 'rahm'

#!/usr/bin/env python

'''
----------------------------------------------------------------------------
 The contents of this file are subject to the "END USER LICENSE AGREEMENT FOR F5
 Software Development Kit for iControl"; you may not use this file except in
 compliance with the License. The License is included in the iControl
 Software Development Kit.

 Software distributed under the License is distributed on an "AS IS"
 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 the License for the specific language governing rights and limitations
 under the License.

 The Original Code is iControl Code and related documentation
 distributed by F5.

 The Initial Developer of the Original Code is F5 Networks,
 Inc. Seattle, WA, USA. Portions created by F5 are Copyright (C) 1996-2004 F5 Networks,
 Inc. All Rights Reserved.  iControl (TM) is a registered trademark of F5 Networks, Inc.

 Alternatively, the contents of this file may be used under the terms
 of the GNU General Public License (the "GPL"), in which case the
 provisions of GPL are applicable instead of those above.  If you wish
 to allow use of your version of this file only under the terms of the
 GPL and not to allow others to use your version of this file under the
 License, indicate your decision by deleting the provisions above and
 replace them with the notice and other provisions required by the GPL.
 If you do not delete the provisions above, a recipient may use your
 version of this file under either the License or the GPL.
----------------------------------------------------------------------------
'''

def sort_ip_dict(ip_list):
    from IPy import IP
    ipl = [ (IP(i[2]['address']).int(),  i) for i in ip_list]
    ipl.sort()
    return [ip[1] for ip in ipl]

def add_tmmRoutes(obj,  filename, pool, vlan):
    pools = pool.get_list()
    vlans = vlan.get_list()
    routefile = open(filename, 'r')
    headers = routefile.readline().strip().split(',')
    rt_hdrs = headers[:1]
    dest_hdrs = headers[1:3]
    atr_hdrs = ['gateway','pool_name','vlan_name']
    rts = []
    dests = []
    atrs = []
    for line in routefile:
        ln = line.strip().split(',')
        rt_name = ln[:1]
        dest = ln[1:3]
        atr = ln[-2:]
        if atr[0] == 'pool':
            if atr[1] in pools:
                atrs.append(dict(zip(atr_hdrs, ['',atr[1],''])))
                dests.append(dict(zip(dest_hdrs, dest)))
                rts.append(rt_name)
            else:
                print "Pool ", atr[1], " does not exist"
        elif atr[0] == 'vlan':
            if atr[1] in vlans:
                atrs.append(dict(zip(atr_hdrs, ['','',atr[1]])))
                dests.append(dict(zip(dest_hdrs, dest)))
                rts.append(rt_name)
            else:
                print "Vlan ", atr[1], " does not exist"
        elif atr[0] == 'gateway':
            atrs.append(dict(zip(atr_hdrs, [atr[1],'',''])))
            dests.append(dict(zip(dest_hdrs, dest)))
            rts.append(rt_name)
        elif atr[0] == 'reject':
            atrs.append(dict(zip(atr_hdrs, ['','',''])))
            dests.append(dict(zip(dest_hdrs, dest)))
            rts.append(rt_name)

    combined = zip(rts, dests, atrs)

    for x in combined:
        xl = list(x)
        obj.create_static_route(routes = xl[0], destinations = [xl[1]], attributes = [xl[2]])

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 %s/%s" % (route['destination'], route['netmask'])

def get_tmmRoutes(obj):

    try:
        tmmStatic = obj.get_static_route_list()
        tmmRtType = obj.get_static_route_type(routes = tmmStatic)
        tmmRtDest = obj.get_static_route_destination(routes = tmmStatic)

    except:
        "Unable to fetch route information - check trace log"

    combined = zip(tmmStatic,  tmmRtType, tmmRtDest)
    combined = [list(a) for a in combined]

    ldict_gw_ip = []
    ldict_gw_pool = []
    ldict_gw_vlan = []
    ldict_gw_reject = []

    for x in combined:
            if x[1] == 'ROUTE_TYPE_GATEWAY':
                x.append(obj.get_static_route_gateway(routes = [x[0]])[0])
                ldict_gw_ip.append(x)
            if x[1] == 'ROUTE_TYPE_POOL':
                x.append(obj.get_static_route_pool(routes = [x[0]])[0])
                ldict_gw_pool.append(x)
            if x[1] == 'ROUTE_TYPE_INTERFACE':
                x.append(obj.get_static_route_vlan(routes = [x[0]])[0])
                ldict_gw_vlan.append(x)
            if x[1] == 'ROUTE_TYPE_REJECT':
                ldict_gw_reject.append(x)

    gw_ip = sort_ip_dict(ldict_gw_ip)
    gw_pool = sort_ip_dict(ldict_gw_pool)
    gw_vlan = sort_ip_dict(ldict_gw_vlan)
    gw_reject = sort_ip_dict(ldict_gw_reject)

    print "\n"*2
    print "TMM IP Routes: (Name: Net/Mask -> Gateway IP)"
    for x in gw_ip:
            print "\t%s: %s/%s -> %s" % (x[0], x[2]['address'], x[2]['netmask'], x[3])

    print "\n"*2
    print "TMM Pool Routes: (Name: Net/Mask -> Gateway Pool)"
    for x in gw_pool:
            print "\t%s: %s/%s -> %s" % (x[0], x[2]['address'], x[2]['netmask'], x[3])

    print "\n"*2
    print "TMM Vlan Routes: (Name: Net/Mask -> Gateway Vlan)"
    for x in gw_vlan:
            print "\t%s: %s/%s -> %s" % (x[0], x[2]['address'], x[2]['netmask'], x[3])

    print "\n"*2
    print "TMM Rejected Routes: (Name: Net/Mask)"
    for x in gw_reject:
            print "\t%s: %s/%s" % (x[0], x[2]['address'], x[2]['netmask'])


if __name__ == "__main__":

        import bigsuds as pc
        import getpass
        import argparse

        parser = argparse.ArgumentParser()

        parser.add_argument("-s",  "--system", required=True)
        parser.add_argument("-u", "--username", required=True)
        parser.add_argument("-d",  "--delRoute")
        parser.add_argument("-a",  "--addRoute")
        args = vars(parser.parse_args())

        print "%s, enter your " % args['username'],
        upass = getpass.getpass()

        b = pc.BIGIP(args['system'], args['username'], upass)

        if args['delRoute']:
            del_tmmRoutes(b.Networking.RouteTable,  args['delRoute'])

        if args['addRoute']:
            add_tmmRoutes(b.Networking.RouteTableV2, args['addRoute'], b.LocalLB.Pool, b.Networking.VLAN)

        get_tmmRoutes(b.Networking.RouteTableV2)
Version history
Last update:
‎09-Mar-2015 13:28
Updated by:
Contributors