#!/usr/bin/python3 # # Connect to the NETCONF server passed on the command line and # display their capabilities. This script and the following scripts # all assume that the user calling the script is known by the server # and that suitable SSH keys are in place. For brevity and clarity # of the examples, we omit proper exception handling. # # $ ./nc01.py broccoli import sys, os, warnings warnings.simplefilter("ignore", DeprecationWarning) from ncclient import manager exec_conf_prefix = """ <__XML__MODE__exec_configure> """ exec_conf_postfix = """ """ cmd_vlan_conf_snippet= """ <__XML__PARAM_value>%s <__XML__MODE_vlan> %s active """ cmd_vlan_int_snippet = """ %s <__XML__MODE_if-ethernet-switch> <__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans> %s """ cmd_no_vlan_int_snippet = """ <__XML__MODE__exec_configure> %s <__XML__MODE_if-ethernet-switch> <__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans> %s """ filter_show_vlan_brief_snippet = """ """ def nxos_connect(host, port, user, password): return manager.connect(host=host, port=port, username=user, password=password, device_params={'name': 'nexus'}) def enable_vlan(mgr, vlanid, vlanname): confstr = cmd_vlan_conf_snippet % (vlanid, vlanname) confstr = exec_conf_prefix + confstr + exec_conf_postfix mgr.edit_config(target='running', config=confstr) def enable_vlan_on_trunk_int(mgr, interface, vlanid): confstr = cmd_vlan_int_snippet % (interface, vlanid) confstr = exec_conf_prefix + confstr + exec_conf_postfix print(confstr) mgr.edit_config(target='running', config=confstr) def disable_vlan_on_trunk_int(mgr, interface, vlanid): confstr = cmd_no_vlan_int_snippet % (interface, vlanid) print(confstr) mgr.edit_config(target='running', config=confstr) def test_nxos_api(host, user, password): with nxos_connect(host, port=22, user=user, password=password) as m: enable_vlan(m, '100', 'customer') enable_vlan_on_trunk_int(m, '2/1', '100') disable_vlan_on_trunk_int(m, '2/1', '100') result = m.get(("subtree", filter_show_vlan_brief_snippet)) # print result if __name__ == '__main__': test_nxos_api(sys.argv[1], sys.argv[2], sys.argv[3])