| •1 |
#!/usr/bin/python3 |
| 2 |
# |
| 3 |
# Connect to the NETCONF server passed on the command line and |
| 4 |
# set a device_params to turn on/off the namespace prefix "nc". |
| 5 |
# if you want to verify the result, you can print the request that |
| 6 |
# was sent. For brevity and clarity of the examples, we omit proper |
| 7 |
# exception handling. |
| 8 |
# |
| 9 |
# $ ./ericsson_nc_prefix_example.py host username password |
| 10 |
|
| 11 |
import sys, os, warnings |
| 12 |
warnings.simplefilter("ignore", DeprecationWarning) |
| 13 |
from ncclient import manager |
| 14 |
|
| 15 |
|
| 16 |
def ericsson_connect(host, port, user, password, device_params): |
| 17 |
return manager.connect(host=host, |
| 18 |
port=port, |
| 19 |
username=user, |
| 20 |
password=password, |
| 21 |
device_params=device_params, |
| 22 |
hostkey_verify-false) |
| 23 |
|
| 24 |
|
| 25 |
def enable_nc_prefix(host, user, password): |
| 26 |
# add a parameter 'with_ns' to turn on/off 'nc' |
| 27 |
device_params = {'name': 'ericsson', 'with_ns': True} |
| 28 |
with ericsson_connect(host, |
| 29 |
port=22, |
| 30 |
user=user, |
| 31 |
password=password, |
| 32 |
device_params=device_params) as m: |
| 33 |
|
| 34 |
ret = m.get_config(source="running").data_xml |
| 35 |
print(ret) |
| 36 |
|
| 37 |
|
| 38 |
def disable_nc_prefix(host, user, password): |
| 39 |
# add a parameter 'with_ns' to turn on/off 'nc' |
| 40 |
device_params = {'name': 'ericsson', 'with_ns': False} |
| 41 |
with ericsson_connect(host, |
| 42 |
port=22, |
| 43 |
user=user, |
| 44 |
password=password, |
| 45 |
device_params=device_params) as m: |
| 46 |
|
| 47 |
ret = m.get_config(source="running").data_xml |
| 48 |
print(ret) |
| 49 |
|
| 50 |
|
| 51 |
def demo(host, user, password): |
| 52 |
enable_nc_prefix(host, user, password) |
| 53 |
print("#"*50) |
| 54 |
disable_nc_prefix(host, user, password) |
| 55 |
|
| 56 |
|
| 57 |
if __name__ == '__main__': |
| 58 |
demo(sys.argv[1], sys.argv[2], sys.argv[3]) |
| 59 |
|