Forum Discussion
Query to retrieve VIP name from Node IP input
Hi, We are doing a DC migration and have the need to retrieve a list of VIP names based on the server/node IP address. I was wondering if anyone has written a script which does this already. I would like to provide an input file with a list of ip addresses and do a loops which retrieves any virtual server to which that server/node IP belongs.
TIA
This should do it, though it is fairly resource-intensive if you have a lot of virtual servers / pools / nodes:
!/usr/bin/env python3 from f5.bigip import ManagementRoot from getpass import getpass from pprint import pprint hostname = 'my.f5.ltm.net' username = 'foo' node_ips = ['192.168.1.1', 192.168.1.2', '192.168.1.3'] mgmt = ManagmentRoot(hostname, username, getpass()) Get node names that have IPs in the list nodes = [] for node in mgmt.tm.ltm.nodes.get_collection(): if node.address in node_ips: fullPath is /PARTITION/NAME nodes.append(node.fullPath) Get pool names which nodes in list belong pools = [] for pool in mgmt.tm.ltm.pools.get_collection(): members = pool.members_s.get_collection() for member in members: Member fullpath is '/PARTITION/NODENAME:SERVICE_PORT' if member.fullPath.split(':')[0] in nodes: pools.append(pool.fullPath) break Get virtual server names which pools in list belong virtuals = [] for virtual in mgmt.tm.ltm.virtuals.get_collection(): if hasattr(virtual, 'pool') and virtual.pool in pools: virtuals.append(virtual.fullPath) pprint(virtuals)
- Sunny_291145Nimbostratus
Hello Paul,
Please go through the below command on your LTM (CLI) where you will get an Excel sheet in var/temp
tmsh show ltm virtual all-properties detail | grep -iE "Ltm::Virtual|Destination|LTM::Pool|IP" | grep -v "Ltm::Virtual Address" | grep -iv "Cipher" | grep -iv "Destination IP Bypasses" | grep -iv "Source IP Bypasses" | grep -iv "Pipelining Data" >> /var/tmp/filename.xlsx
This should do it, though it is fairly resource-intensive if you have a lot of virtual servers / pools / nodes:
!/usr/bin/env python3 from f5.bigip import ManagementRoot from getpass import getpass from pprint import pprint hostname = 'my.f5.ltm.net' username = 'foo' node_ips = ['192.168.1.1', 192.168.1.2', '192.168.1.3'] mgmt = ManagmentRoot(hostname, username, getpass()) Get node names that have IPs in the list nodes = [] for node in mgmt.tm.ltm.nodes.get_collection(): if node.address in node_ips: fullPath is /PARTITION/NAME nodes.append(node.fullPath) Get pool names which nodes in list belong pools = [] for pool in mgmt.tm.ltm.pools.get_collection(): members = pool.members_s.get_collection() for member in members: Member fullpath is '/PARTITION/NODENAME:SERVICE_PORT' if member.fullPath.split(':')[0] in nodes: pools.append(pool.fullPath) break Get virtual server names which pools in list belong virtuals = [] for virtual in mgmt.tm.ltm.virtuals.get_collection(): if hasattr(virtual, 'pool') and virtual.pool in pools: virtuals.append(virtual.fullPath) pprint(virtuals)
- paul_dawson_258Nimbostratus
Thanks Jason, how can I output them on one line i.e. [node_ip, node, pool, virtual].
I'm not strong on Python.
Give this version a try. It will print the same virtual server / pool multiple times - one for each member (node) with an IP in the list. Please make sure to spot check this before relying on its data as it was done quickly and I could have easily overlooked some detail.
!/usr/bin/env python3 from f5.bigip import ManagementRoot from getpass import getpass from pprint import pprint hostname = 'my.f5.ltm.net' username = 'foo' node_ips = ['192.168.1.1', '192.168.1.2', '192.168.1.3'] mgmt = ManagmentRoot(hostname, username, getpass()) Get node names that have IPs in the list nodes = {} for node in mgmt.tm.ltm.nodes.get_collection(): if node.address in node_ips: fullPath is /PARTITION/NAME (or /PARTITION/SUB_PATH/NAME) nodes[node.fullPath] = node.address Get pool names which nodes in list belong pools = {} for pool in mgmt.tm.ltm.pools.get_collection(): members = pool.members_s.get_collection() for member in members: Member fullpath is /PARTITION/NODENAME:SERVICE_PORT (or /PARTITION/SUB_PATH/NODENAME:SERVER_PORT) if member.fullPath.split(':')[0] in nodes: if pool.fullPath in pools: pools[pool.fullPath].append(member.fullPath.split(':')[0]) else: pools[pool.fullPath] = [member.fullPath.split(':')[0]] Get virtual server names which pools in list belong virtuals = {} for virtual in mgmt.tm.ltm.virtuals.get_collection(): if hasattr(virtual, 'pool') and virtual.pool in pools: virtuals[virtual.fullPath] = virtual.pool Print CSV print('Node IP,Node,Pool,Virtual') Use iteritems() instead of items() with Python 2.x for virtual, pool in virtuals.items(): for node in pools[pool]: print('{},{},{},{}'.format( nodes[node], node, pool, virtual, ))
- paul_dawson_258Nimbostratus
Thanks Jason this is extremely helpful and working. I just have one last question. As we are using route domains and there is a % in the node_ip, we have to put the route domain in for the script to find the associated Pools/VIPs etc. Is there any way to query the DB without having to include the partition in the node_ip?
Thanks again for your help.
- Jason_NanceNimbostratus
This should do it, though it is fairly resource-intensive if you have a lot of virtual servers / pools / nodes:
!/usr/bin/env python3 from f5.bigip import ManagementRoot from getpass import getpass from pprint import pprint hostname = 'my.f5.ltm.net' username = 'foo' node_ips = ['192.168.1.1', 192.168.1.2', '192.168.1.3'] mgmt = ManagmentRoot(hostname, username, getpass()) Get node names that have IPs in the list nodes = [] for node in mgmt.tm.ltm.nodes.get_collection(): if node.address in node_ips: fullPath is /PARTITION/NAME nodes.append(node.fullPath) Get pool names which nodes in list belong pools = [] for pool in mgmt.tm.ltm.pools.get_collection(): members = pool.members_s.get_collection() for member in members: Member fullpath is '/PARTITION/NODENAME:SERVICE_PORT' if member.fullPath.split(':')[0] in nodes: pools.append(pool.fullPath) break Get virtual server names which pools in list belong virtuals = [] for virtual in mgmt.tm.ltm.virtuals.get_collection(): if hasattr(virtual, 'pool') and virtual.pool in pools: virtuals.append(virtual.fullPath) pprint(virtuals)
- paul_dawson_258Nimbostratus
Thanks Jason, how can I output them on one line i.e. [node_ip, node, pool, virtual].
I'm not strong on Python.
- Jason_NanceNimbostratus
Give this version a try. It will print the same virtual server / pool multiple times - one for each member (node) with an IP in the list. Please make sure to spot check this before relying on its data as it was done quickly and I could have easily overlooked some detail.
!/usr/bin/env python3 from f5.bigip import ManagementRoot from getpass import getpass from pprint import pprint hostname = 'my.f5.ltm.net' username = 'foo' node_ips = ['192.168.1.1', '192.168.1.2', '192.168.1.3'] mgmt = ManagmentRoot(hostname, username, getpass()) Get node names that have IPs in the list nodes = {} for node in mgmt.tm.ltm.nodes.get_collection(): if node.address in node_ips: fullPath is /PARTITION/NAME (or /PARTITION/SUB_PATH/NAME) nodes[node.fullPath] = node.address Get pool names which nodes in list belong pools = {} for pool in mgmt.tm.ltm.pools.get_collection(): members = pool.members_s.get_collection() for member in members: Member fullpath is /PARTITION/NODENAME:SERVICE_PORT (or /PARTITION/SUB_PATH/NODENAME:SERVER_PORT) if member.fullPath.split(':')[0] in nodes: if pool.fullPath in pools: pools[pool.fullPath].append(member.fullPath.split(':')[0]) else: pools[pool.fullPath] = [member.fullPath.split(':')[0]] Get virtual server names which pools in list belong virtuals = {} for virtual in mgmt.tm.ltm.virtuals.get_collection(): if hasattr(virtual, 'pool') and virtual.pool in pools: virtuals[virtual.fullPath] = virtual.pool Print CSV print('Node IP,Node,Pool,Virtual') Use iteritems() instead of items() with Python 2.x for virtual, pool in virtuals.items(): for node in pools[pool]: print('{},{},{},{}'.format( nodes[node], node, pool, virtual, ))
- paul_dawson_258Nimbostratus
Thanks Jason this is extremely helpful and working. I just have one last question. As we are using route domains and there is a % in the node_ip, we have to put the route domain in for the script to find the associated Pools/VIPs etc. Is there any way to query the DB without having to include the partition in the node_ip?
Thanks again for your help.
- shabuboyAltostratus
Thank you @Jason Nance!!!
I was looking into creating a script to find a node IP in devices and ran into this, works great!
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com