Configure an interface

Python Path


In [28]:
import sys
sys.path.append("..")

Environment


In [29]:
from os import environ
environ["NETWORK_PROFILE"] = "giles"

Select Device

The following code chooses one interface on device.


In [30]:
from basics.sample import sample_interface

(device_name, interface_name) = sample_interface()
print 'Use', (device_name, interface_name)


Use ('xrvr-b', 'GigabitEthernet0/0/0/3')

Obtain Configuration


In [25]:
from basics.interface_configuration import interface_configuration
interface = interface_configuration(device_name, interface_name)

Show initial configuration


In [26]:
table_md_template = '''
    |Name|Description|Shutdown|Address|NetMask|
    |:-|:-|:-:|:-|:-|
    |%s|%s|%s|%s|%s|
'''

table_md = table_md_template % (
        interface.name,
        interface.description,
        interface.shutdown,
        interface.address,
        interface.netmask)
    
# from basics.markdown import normtable
# print normtable(table_md)
print table_md


    |Name|Description|Shutdown|Address|NetMask|
    |:-|:-|:-:|:-|:-|
    |GigabitEthernet0/0/0/3|hiya|False|101.101.101.101|255.255.0.0|

Change description, IP address and mask


In [27]:
from basics.interface_configuration import interface_configuration_update

interface_configuration_update(device_name, interface_name, description='Formerly ' + interface.address,
                                   address='101.101.101.101', netmask='255.255.0.0', shutdown=interface.shutdown)

Show modified configuration


In [12]:
modified = interface_configuration(device_name, interface_name)
table_md = table_md_template % (
        modified.name,
        modified.description,
        modified.shutdown,
        modified.address,
        modified.netmask)
    
# from basics.markdown import normtable
# print normtable(table_md)
print table_md


    |Name|Description|Shutdown|Address|NetMask|
    |:-|:-|:-:|:-|:-|
    |GigabitEthernet0/0/0/3|hiya|False|101.101.101.101|255.255.0.0|

Restore configuration


In [9]:
interface_configuration_update(device_name, interface_name, description=interface.description,
                                   address=interface.address, netmask=interface.netmask, shutdown=interface.shutdown)

Show restored configuration


In [14]:
restored = interface_configuration(device_name, interface_name)
table_md = table_md_template % (
        restored.name,
        restored.description,
        restored.shutdown,
        restored.address,
        restored.netmask)
    
# from basics.markdown import normtable
# print normtable(table_md)
print table_md


    |Name|Description|Shutdown|Address|NetMask|
    |:-|:-|:-:|:-|:-|
    |GigabitEthernet0/0/0/3|hiya|False|101.101.101.101|255.255.0.0|

Shutdown interface


In [ ]:
interface_configuration_update(device_name, interface_name, interface.description,
                                   interface.address, interface.netmask, shutdown=True)

Show shutdown configuration


In [16]:
shutdown = interface_configuration(device_name, interface_name)
table_md = table_md_template % (
        shutdown.name,
        shutdown.description,
        shutdown.shutdown,
        shutdown.address,
        shutdown.netmask)
    
# from basics.markdown import normtable
# print normtable(table_md)
print table_md


    |Name|Description|Shutdown|Address|NetMask|
    |:-|:-|:-:|:-|:-|
    |GigabitEthernet0/0/0/3|hiya|False|101.101.101.101|255.255.0.0|

Start interface


In [17]:
interface_configuration_update(device_name, interface_name, interface.description,
                                   interface.address, interface.netmask, shutdown=False)

Show started configuration


In [18]:
startup = interface_configuration(device_name, interface_name)
table_md = table_md_template % (
        startup.name,
        startup.description,
        startup.shutdown,
        startup.address,
        startup.netmask)
    
# from basics.markdown import normtable
# print normtable(table_md)
print table_md


    |Name|Description|Shutdown|Address|NetMask|
    |:-|:-|:-:|:-|:-|
    |GigabitEthernet0/0/0/3|hiya|False|101.101.101.101|255.255.0.0|