In [1]:
table {float:left}

Working with IP Scope Objects

So now we're going to look at the recently implemented IP Scope Objects and supporting capabilities in the pyhpeimc library

In [2]:
import csv
import time
from pyhpeimc.auth import *
from pyhpeimc.objects import *

In [3]:
auth = IMCAuth("http://", "", "8080", "admin", "admin")

Viewing the Parent Scope

Say we have an existing IP address scope in the HPE IMC server. You would find this under the Reource > Terminal Access > IP Address Allocation page. We've got a pretty typical setup here where we've got a large parent scope which is assigned to a large IP address range specifically a /16 ( ). This gives us a LOT of ip addresses to work with for this site, but it probably needs to be broken down into child segments which are simply IP networks which fit into the parent range.

For our examples, we're going to be looking at the parent range and the child of which, of course, fits cleanly inside the parent range. Everyone remembers basic subneting right?


The first thing we're going to do is to create our first parent object. To do this we will instantiate a new instance of the IPScope class, passing it the parent subnet, and the creds and url attributes from the auth object we created above.

In [4]:
mainscope = IPScope('', auth.creds, auth.url)

What can we do with this?

Now that the object exists, it's got a bunch of attributes and methods which we can play around with. Remember, you can always dir(object) in python to be able to see what attributes and methods are available for a given object. Never a bad thing if you're just poking around or a little bit forgetful.

For this object, we're just going to get right into it and print off some of the attributes.

In [5]:
print (mainscope.startip)
print (mainscope.endip)
print (
print (mainscope.netaddr)

As you can see, the starting address for this scope is and the last address is This is exactly what you would expect for a /16 right?

We can also see the id, which is going to be used internally by the python library and the IMC REST API to be able to identify the specific scope we want to work with.

The last attribute is simply the network address of the object. Always good to make sure that we did what we thought we wanted to do.

Remember I said above that this Parent scope had some child scopes underneath it? Let's take a look at one of those.

In [6]:
print (" There are: " + str(len(mainscope.child)) + " children scopes \n\n Let's look at the first one \n")

 There are: 4 children scopes 

 Let's look at the first one 

{'description': 'default production segment',
 'endIp': '',
 'id': '67',
 'ip': '',
 'name': 'cyoung',
 'parentId': '48',
 'percent': '0.40551181102362205',
 'percentStr': '40.55%',
 'rightIpId': '48',
 'startIp': ''}

A Child is a beautiful thing

We can see that the first child, (remember python starts counting at 0 right?), has the range Let's go ahead and create a new childscope object to check it out.

In [7]:
childscope = IPScope('', auth.creds, auth.url)

In [8]:
print (childscope.startip)
print (childscope.endip)
print (
print (childscope.netaddr)

Not the same thing

You can clearly see that the new childscope object has the starting ip of and the ending ip of You can also see that the id of this is different that the parentscope, which means that they are clearly two different objects.

Now that we've got a much smaller range, let's take a look at some of the additional methods that are available for any IPScope object.

For example, maybe you want to know the next unassigned IP address range in your scope. You could use the nextfreeip method to be able to pull the next unassigned IP address from the eligible hosts in that specific IP Scope.

Let's try that now:

In [9]:


So we can clearly see that the next available IP address is in this /24. Right?

Maybe we have a brand new piece of kit for our lab and we need to assign a management IP address for it in this range, well now we have the IP address without having to check out any spreadsheets, although you MAY want to PING the address just to make sure your co-worker didn't forget to update the database, cause you would NEVER do that, right?

Now that we've got the new address, let's immediately allocate it in the pool so that we don't cause any problems for co-workers in the future.

In [12]:
childscope.allocateIp('', 'netmanchris', 'Juniper')

Now that we've added this new IP address allocation to the 10.101.0./24 subnet, let's update the hosts list for the childscope object

In [13]:

In [14]:
for i in childscope.hosts:
    if i['ip'] == '':
        print (i)

{'id': '161', 'name': 'netmanchris', 'ip': '', 'parentId': '67', 'description': 'Juniper'}

Cleaning Up

Now that we've succesfully added the new IP allocation to the IMC System, it's time to clean up after ourselves.

To do that, we're simply going to take the id attribute of the IP allocation we created above and use that as the input to the deallocateIP method of the childscope IPSCope object.

This should successfully delete the IP allocation that we just created and our system is back to normal.

Cool right?

For the last step, we wil refresh the hosts list for the childscope object again and make sure that the Juniper record we created above at IP is, indeed, gone.

In [16]:

In [17]:
for i in childscope.hosts:
    if i['ip'] == '':
        print (i)


If you've got questions, concerns, or comments, please feel free to post below