Skip to content

Instantly share code, notes, and snippets.

@jeremyschulman
Created September 25, 2013 13:40
Show Gist options
  • Save jeremyschulman/6699751 to your computer and use it in GitHub Desktop.
Save jeremyschulman/6699751 to your computer and use it in GitHub Desktop.
python example to change Junos port admin state
import sys
from ncclient import manager as ncclient
from ncclient.operations.rpc import RPCError
from lxml import etree
### ---------------------------------------------------------------------------
### unix invocation:
### $ python change_interface_admin.py <hostname> <portname> [up|down]
###
### example:
### $ python change_interface_admin.py 192.168.10.21 ge-0/0/1 down
### ---------------------------------------------------------------------------
arg_host = sys.argv[1]
arg_interface_name = sys.argv[2]
arg_admin_state = False if sys.argv[3] == 'down' else True
username = "root"
password = "juniper1"
### -----------------------------------------------------------------
### build_rpc_cmd - helper function to create XML API command
### -----------------------------------------------------------------
def build_rpc_cmd( rpc_cmd, rpc_args ):
"""
rpc_cmd - str: command
rpc_args - dict: key/value rpc arguments
"""
rpc = etree.Element( rpc_cmd )
if rpc_args:
for name, value in rpc_args.items():
arg = etree.SubElement( rpc, name )
if value != True: arg.text = value
return rpc
### -----------------------------------------------------------------
### set_port_admin - change the port admin state
### -----------------------------------------------------------------
def set_port_admin( ncdev, port_name=arg_interface_name, admin_state=arg_admin_state ):
"""
Change the admin state of a port.
ncdev - netconf device object
port_name - str: name of the interface
admin_state - True (enable), False (disable)
Notes:
(1) change is exclusive
(2) exceptions are bubbled up
"""
ncdev.lock()
# disabling a port means setting 'disable' on the port
set_config = "{cmd} interfaces {name} disable".format(
name=port_name,
cmd=('set' if not admin_state else 'delete'))
# not trapping on RPCError; should one occur, it could
# be cause by the calling routine.
rsp = ncdev.load_configuration(action='set', config=set_config)
ncdev.commit()
ncdev.unlock()
return admin_state
### -----------------------------------------------------------------
### get_port_status - used to retrieve basic port information
### -----------------------------------------------------------------
def get_port_status( ncdev, port_name=arg_interface_name ):
"""
get basic port information, returns a dict of information
or False if the provided interface name (ifd_name) does
not exist on the device
ncdev - netconf device object
port_name - str: the name of the interface, e.g. 'ge-0/0/0'
"""
cmd = build_rpc_cmd('get-interface-information', {
'interface-name': port_name,
'media': True
})
rpc = ncdev.rpc( cmd )
try:
ifd_xml = rpc.xpath('//physical-interface')[0]
except IndexError:
# indicates that interface does not exist on the device
return False
xpath_item = lambda xpath: ifd_xml.xpath(xpath)[0].text.strip()
status = {}
status['name'] = port_name
status['status'] = xpath_item('oper-status')
status['admin'] = xpath_item('admin-status')
status['speed'] = xpath_item('speed')
status['duplex'] = xpath_item('duplex')
status['mtu'] = xpath_item('mtu')
return status
if __name__ == "__main__":
ncdev = ncclient.connect(host=arg_host, username=username, password=password, timeout=10, hostkey_verify=False )
set_port_admin( ncdev )
ncdev.close_session()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment