| •1 |
#!/usr/bin/python3 |
| 2 |
# |
| 3 |
# Connect to the NETCONF server passed on the command line and |
| 4 |
# display their capabilities. This script and the following scripts |
| 5 |
# all assume that the user calling the script is known by the server |
| 6 |
# and that suitable SSH keys are in place. For brevity and clarity |
| 7 |
# of the examples, we omit proper exception handling. |
| 8 |
# |
| 9 |
# $ ./nc01.py broccoli |
| 10 |
|
| 11 |
import sys, os, warnings |
| 12 |
warnings.simplefilter("ignore", DeprecationWarning) |
| 13 |
from ncclient import manager |
| 14 |
|
| 15 |
exec_conf_prefix = """ |
| 16 |
<nc:config xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 17 |
<configure xmlns="http://www.cisco.com/nxos:1.0:vlan_mgr_cli"> |
| 18 |
<__XML__MODE__exec_configure> |
| 19 |
""" |
| 20 |
|
| 21 |
exec_conf_postfix = """ |
| 22 |
</__XML__MODE__exec_configure> |
| 23 |
</configure> |
| 24 |
</nc:config> |
| 25 |
""" |
| 26 |
|
| 27 |
cmd_vlan_conf_snippet= """ |
| 28 |
<vlan> |
| 29 |
<vlan-id-create-delete> |
| 30 |
<__XML__PARAM_value>%s</__XML__PARAM_value> |
| 31 |
<__XML__MODE_vlan> |
| 32 |
<name> |
| 33 |
<vlan-name>%s</vlan-name> |
| 34 |
</name> |
| 35 |
<state> |
| 36 |
<vstate>active</vstate> |
| 37 |
</state> |
| 38 |
<no> |
| 39 |
<shutdown/> |
| 40 |
</no> |
| 41 |
</__XML__MODE_vlan> |
| 42 |
</vlan-id-create-delete> |
| 43 |
</vlan> |
| 44 |
""" |
| 45 |
|
| 46 |
cmd_vlan_int_snippet = """ |
| 47 |
<interface> |
| 48 |
<ethernet> |
| 49 |
<interface>%s</interface> |
| 50 |
<__XML__MODE_if-ethernet-switch> |
| 51 |
<switchport> |
| 52 |
<trunk> |
| 53 |
<allowed> |
| 54 |
<vlan> |
| 55 |
<add> |
| 56 |
<__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans> |
| 57 |
<add-vlans>%s</add-vlans> |
| 58 |
</__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans> |
| 59 |
</add> |
| 60 |
</vlan> |
| 61 |
</allowed> |
| 62 |
</trunk> |
| 63 |
</switchport> |
| 64 |
</__XML__MODE_if-ethernet-switch> |
| 65 |
</ethernet> |
| 66 |
</interface> |
| 67 |
""" |
| 68 |
|
| 69 |
cmd_no_vlan_int_snippet = """ |
| 70 |
<nc:config xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 71 |
<configure xmlns="http://www.cisco.com/nxos:1.0:vlan_mgr_cli"> |
| 72 |
<__XML__MODE__exec_configure> |
| 73 |
<interface> |
| 74 |
<ethernet> |
| 75 |
<interface>%s</interface> |
| 76 |
<__XML__MODE_if-ethernet-switch> |
| 77 |
<switchport> |
| 78 |
<trunk> |
| 79 |
<allowed> |
| 80 |
<vlan> |
| 81 |
<remove> |
| 82 |
<__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans> |
| 83 |
<remove-vlans>%s</remove-vlans> |
| 84 |
</__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans> |
| 85 |
</remove> |
| 86 |
</vlan> |
| 87 |
</allowed> |
| 88 |
</trunk> |
| 89 |
</switchport> |
| 90 |
</__XML__MODE_if-ethernet-switch> |
| 91 |
</ethernet> |
| 92 |
</interface> |
| 93 |
</__XML__MODE__exec_configure> |
| 94 |
</configure> |
| 95 |
</nc:config> |
| 96 |
""" |
| 97 |
|
| 98 |
filter_show_vlan_brief_snippet = """ |
| 99 |
<show xmlns="http://www.cisco.com/nxos:1.0:vlan_mgr_cli"> |
| 100 |
<vlan> |
| 101 |
<brief/> |
| 102 |
</vlan> |
| 103 |
</show> """ |
| 104 |
|
| 105 |
def nxos_connect(host, port, user, password): |
| 106 |
return manager.connect(host=host, port=port, username=user, |
| 107 |
password=password, device_params={'name': 'nexus'}) |
| 108 |
|
| 109 |
|
| 110 |
def enable_vlan(mgr, vlanid, vlanname): |
| 111 |
confstr = cmd_vlan_conf_snippet % (vlanid, vlanname) |
| 112 |
confstr = exec_conf_prefix + confstr + exec_conf_postfix |
| 113 |
mgr.edit_config(target='running', config=confstr) |
| 114 |
|
| 115 |
|
| 116 |
def enable_vlan_on_trunk_int(mgr, interface, vlanid): |
| 117 |
confstr = cmd_vlan_int_snippet % (interface, vlanid) |
| 118 |
confstr = exec_conf_prefix + confstr + exec_conf_postfix |
| 119 |
print(confstr) |
| 120 |
mgr.edit_config(target='running', config=confstr) |
| 121 |
|
| 122 |
|
| 123 |
def disable_vlan_on_trunk_int(mgr, interface, vlanid): |
| 124 |
confstr = cmd_no_vlan_int_snippet % (interface, vlanid) |
| 125 |
print(confstr) |
| 126 |
mgr.edit_config(target='running', config=confstr) |
| 127 |
|
| 128 |
|
| 129 |
def test_nxos_api(host, user, password): |
| 130 |
with nxos_connect(host, port=22, user=user, password=password) as m: |
| 131 |
enable_vlan(m, '100', 'customer') |
| 132 |
enable_vlan_on_trunk_int(m, '2/1', '100') |
| 133 |
disable_vlan_on_trunk_int(m, '2/1', '100') |
| 134 |
result = m.get(("subtree", filter_show_vlan_brief_snippet)) |
| 135 |
# print result |
| 136 |
|
| 137 |
|
| 138 |
if __name__ == '__main__': |
| 139 |
test_nxos_api(sys.argv[1], sys.argv[2], sys.argv[3]) |