Forum Discussion

KernelPanic's avatar
KernelPanic
Icon for Nimbostratus rankNimbostratus
Nov 29, 2017

F5-SDK How can I iterate a list of hosts with ManagementRoot(hostname)

When I iterate a list of hosts the ManagementRoot(hostname, username, password, token) class uses the literal and not the variable value for hostname. I only find single static IP address used in the hostname parameter SDK examples here. It is easily done with curl.

Would someone show me how this is done in the F5 SDK?

f5adminloop.py

import requests
from f5.bigip import ManagementRoot
requests.packages.urllib3.disable_warnings()  ignore cert warnings

c = ManagementRoot('10.2.92.140',"admin","admin",token=True)
print(c.tmos_version)

f5List = ['10.2.192.140', '10.222.224.72', '10.222.123.32', '10.222.123.33']
for f5 in f5List:
    f5 = "'" + f5 + "'"
    b = 'ManagementRoot(%s,"admin","admin",token=True)' % (f5)
    print(b)
    print(b.tmos_version)

[root@flks-centos7-01 scripts] python f5adminloop.py
/usr/lib/python2.7/site-packages/urllib3/connectionpool.py:769: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
  InsecureRequestWarning)
12.1.2
ManagementRoot('10.2.192.140',"admin","admin",token=True)
Traceback (most recent call last):
  File "f5adminloop.py", line 17, in 
    print(b.tmos_version)
AttributeError: 'str' object has no attribute 'tmos_version'

3 Replies

  • Your 'b' line is constructing a string rather than calling the function. Try this:

    for f5 in f5List:
        b = ManagementRoot(f5,"admin","admin",token=True)
        print(b)
        print(b.tmos_version)
    
  • with open("f5devices.txt") as file:
        stuff = file.read().splitlines()
        for thing in stuff:
            print(thing)
            b = ManagementRoot(thing, 'admin', 'admin', token=False)
            print(b.tmos_version)
            u = b.tm.auth.users.get_collection()
            for usr in u:
                print(usr.name)
            delusr_lst = ["meh", "yew", "kittie"]
            for delusr in delusr_lst:
                 if b.tm.auth.users.user.exists(name=delusr):
                       print(delusr + " exists on system...deleting now!")
                       rmusr = b.tm.auth.users.user.load(name=delusr)
                       rmusr.delete()
                 else:
                       print(" no user " + delusr + " on system...skipping!")
            p = b.tm.auth.users.get_collection()
            for usr in p:
                print(usr.name)