Forum Discussion

Steve_Brown_882's avatar
Steve_Brown_882
Historic F5 Account
Jul 01, 2009

pycontrol help

Hi Guys,

 

I am pretty new to python and pycontrol and need a little help with a script I am working on. I think I am missibng something obvious and this may even be more a case of me not fully understanding pyton yet. Any how I would like to have my script return a list of virtual servers, destination IP and destination port. Here is a snippet of my current code which works, but does not return exactly what I am lookign for.

 

 

virtual = b.LocalLB_VirtualServer

 

 

for vName in virtual.get_list()['return']:

 

vDest = virtual.get_destination(virtual_servers = [vName])['return']

 

print vName, vDest

 

 

The output looks like this....

 

 

vipname [{'port': 443, 'address': '10.1.2.3'}

 

 

And I want it to look like...I assume I need to pull the port: and address out of the return, but I have not been able to figure out how.

 

 

vipname 443 10.1.2.3

4 Replies

  • Steve_Brown_882's avatar
    Steve_Brown_882
    Historic F5 Account
    Just wanted to add the solution since I just figured it out.

     

     

    The code now looks like this...

     

     

    import pycontrol.pyControl as pyControl

     

    host = 'x.x.x.x'

     

    uname = 'username'

     

    upass = 'password'

     

     

    b = pyControl.BIGIP(

     

    hostname = host,

     

    username = uname,

     

    password = upass,

     

    wsdl_files = ['LocalLB.VirtualServer']

     

    )

     

     

    virtual = b.LocalLB_VirtualServer

     

     

    for vName in virtual.get_list()['return']:

     

    vDest = virtual.get_destination(virtual_servers = [vName])['return']

     

    vPort = vDest[0]['port']

     

    vIP = vDest[0]['address']

     

    vPool = virtual.

     

    print vName,",",vIP,",",vPort
  • Thanks for posting your update! I'm very weak on my python skills so this will be a good reference to point folks to.

     

     

    -Joe
  • Good post, thanks for sharing. Here's another method: it looks like you're calling the iControl portal for each virtual server name separately. iControl and Python's zip() function can save you (and the BigIP!) some time and CPU cycles:

      
     In [16]: vlist = b.LocalLB_VirtualServer.get_list()['return']  
     In [18]: vdest = b.LocalLB_VirtualServer.get_destination(virtual_servers = vlist)['return']  
     In [19]: combined = zip(vlist, vdest)  
     In [20]: for x in combined:  
     ....:     print "%s=>%s:%d" % (x[0], x[1]['address'], x[1]['port'])  
     

    Produces output like: virtual_forward_test=>192.168.1.0:0

    The key bit here is that you can pass in the entire list of VS names to get_destination, and iControl will do the Right Thing and return the ip/port combos back in order. Then you "zip" them together, which will create a tuple by default - you then just iterate through it as if it were a list...

    I hope this helps!

    -Matt
  • Steve_Brown_882's avatar
    Steve_Brown_882
    Historic F5 Account
    Thanks for the input Matt. I have incorporated some of you suggestions and it sure runs faster. I also changed the prints because my ultimate goal was to create a CSV to help us create some documentation. Here is the newest version in case anyone wants to see. I am sure it is not the cleanest code but it is still a work in progress.

     

     

    virtual = b.LocalLB_VirtualServer

     

    pool = b.LocalLB_Pool

     

     

     

    vlist = virtual.get_list()['return']

     

    state = virtual.get_enabled_state(virtual_servers = vlist)['return']

     

    vdest = virtual.get_destination(virtual_servers = vlist)['return']

     

    vpool = virtual.get_default_pool_name(virtual_servers = vlist)['return']

     

    member = pool.get_member(pool_names = vpool)['return']

     

     

    combined = zip(vlist, state, vdest)

     

     

     

    file = open("BigIP.csv", "w")

     

    file.write('Virtual Server Name,Virtual Server State,Destination IP,Destination Port')

     

    file.write("\n")

     

    file.close()

     

     

    for x in combined:

     

    details = x[0]+ "," + x[1] + "," + str(x[2]['address']) + "," + str(x[2]['port'])

     

    file = open("BigIP.csv", "a")

     

    file.write(details)

     

    file.write("\n")

     

    file.close