09-08-2017 08:33 AM - edited 03-01-2019 03:59 AM
I have the following python code. I get an allocation that I can see when I do show resource-pools but I am not able to convert the "net" object into a string or ipaddress object.
pool_name = "LOOPS"
alloc_name = service.name
self.log.info("Allocating Loopback IP")
ip_allocator.net_request(service,
"/services/leaf-connectivity:leaf-connectivity[name='%s']" % (service.name),
tctx.username,
pool_name,
alloc_name,
32)
net = ip_allocator.net_read(tctx.username, root, pool_name, alloc_name)
if not net:
self.log.info("Alloc not ready")
return
--> loopback_net = ipaddress.ip_network(str(net)) <--THIS IS WHERE IT BREAKS
Does anybody have an example?
Solved! Go to Solution.
09-08-2017 10:34 AM
I've found that (in python) using the "ipaddress" module makes many tasks easy.
from the Resource Allocator, i've done this:
import ipaddress
#...skip
tempip = ipaddress.IPv4Network(unicode(net), strict=True)
vrtrpubip = tempip.network_address # e.g. 10.87.163.146
you can find details on all the things "ipaddress" can do here:
https://docs.python.org/3/howto/ipaddress.html
here are a few that i've used... (as another example)
insideVLN = ipaddress.ip_interface(unicode(inVlannet))
insideVLNip = insideVLN.ip
insideVLNnet = insideVLN.network
insideVLNnetGW = insideVLNnet[1]
hth
gregg
09-08-2017 10:34 AM
I've found that (in python) using the "ipaddress" module makes many tasks easy.
from the Resource Allocator, i've done this:
import ipaddress
#...skip
tempip = ipaddress.IPv4Network(unicode(net), strict=True)
vrtrpubip = tempip.network_address # e.g. 10.87.163.146
you can find details on all the things "ipaddress" can do here:
https://docs.python.org/3/howto/ipaddress.html
here are a few that i've used... (as another example)
insideVLN = ipaddress.ip_interface(unicode(inVlannet))
insideVLNip = insideVLN.ip
insideVLNnet = insideVLN.network
insideVLNnetGW = insideVLNnet[1]
hth
gregg
09-08-2017 10:43 AM
Thanks for replying, I actually st got it while trying a few things.
I was using the ipaddress module but was not converting the allocated network to utf-8 while passing it and I was getting an error.
It is working node using the following:
net = ip_allocator.net_read(tctx.username, root, pool_name, alloc_name)
ipaddress.ip_network(unicode(net, "utf-8")
service.leaf_loopback_ip = str(ip)
01-08-2020 02:35 AM
If you want to get the IP address of whichever interface is used to connect to the network without having to know its name, you can use this:
import socket
def get_ip_address():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
return s.getsockname()[0]
You do not have to have a route to 8.8.8.8 to use this. All it is doing is opening a socket, but not sending any data.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide