Goal

This tutorial gets and displays interface addresses and prefixes, and sets interface addresses.

Tutorial Code

The code used in this tutorial is available in the InterfaceAddressTutorial.py file located under <SDK Location>/python/tutorials/interfaces/InterfaceAddressTutorial.py.

Requirements/Prerequisites

To connect to a network element, an application must have the following information available:

Steps In Detail

Get and Log Interface Addresses

Create a list of all the interfaces on the onePK-enabled network element, logged as addresses.

def logInterfaceAddresses(self, networkInterfaces):
    print "\nPrinting interface address..."
    addresses = None
    for networkInterface in networkInterfaces:
        addresses = networkInterface.get_address_list()
        for address in addresses:
            if address != None:
                print "Interface - " + networkInterface.name + "\tAddress:" + address

Get and Log Interface Prefixes

Create a list of all the network interface prefixes on the onePK-enabled network element, logged as prefixes.

def logInterfacePrefixes(self, networkInterfaces):
    print "\nPrinting interface prefix..."
    prefixes = None
    for networkInterface in networkInterfaces:
        prefixes = networkInterface.get_prefix_list()
        for prefix in prefixes:
            if prefix != None:
                print "Interface - " + networkInterface.name + "\tPrefix:" + str(prefix.prefix_length)

Set Interface Address

Assign an address to a specific network interface. In this case, the code cycles through all of the network interfaces on a network element and reassigns their previous address. Using the same concept, we can assign any address to any network interface on a specific network element.

    def setInterfaceIPAddress(self, networkInterface):
        if networkInterface == None:
            logger.error("No interfaces are available.")
            return None
        setPrefix = 24;
        setAddr = None
        
        prefixes = networkInterface.get_prefix_list()
	for prefix in prefixes:
		if prefix:
			setPrefix = prefix.prefix_length
			setAddr = prefix.address
			if HostIpCheck(setAddr).is_ipv4():
				print "\nSetting IPv4 address of interface " + networkInterface.name
				networkInterface.set_address(1,OnepAddressScopeType.ONEP_ADDRESS_IPv4_PRIMARY,setAddr,setPrefix)
				print "Successfully set the IP address of interface " + networkInterface.name 
				print " to " + setAddr + " prefix " + str(setPrefix)
			break

Result

Congratulations! You now know how to display the prefixes and addresses of network interfaces. You also now know how to assign an address to any network interface on a onePK-enabled network element.