cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2067
Views
2
Helpful
3
Replies

How to use resource-manager ip_allocator in python

jfloresr
Cisco Employee
Cisco Employee

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?

1 Accepted Solution

Accepted Solutions

gschudel
Cisco Employee
Cisco Employee

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


View solution in original post

3 Replies 3

gschudel
Cisco Employee
Cisco Employee

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


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)

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.

Polls
AI-powered tools for network troubleshooting are likely to be part of everyone’s workflow sooner or later. What is the single biggest challenge or concern you see with adopting these tools in your organization?