File: ericsson_nc_prefix_example.py

package info (click to toggle)
python-ncclient 0.6.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,448 kB
  • sloc: python: 9,548; xml: 476; makefile: 77; sh: 5
file content (59 lines) | stat: -rw-r--r-- 1,908 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python3
#
# Connect to the NETCONF server passed on the command line and
# set a device_params to turn on/off the namespace prefix "nc".
# if you want to verify the result, you can print the request that
# was sent. For brevity and clarity of the examples, we omit proper
# exception handling.
#
# $ ./ericsson_nc_prefix_example.py host username password

import sys, os, warnings
warnings.simplefilter("ignore", DeprecationWarning)
from ncclient import manager


def ericsson_connect(host, port, user, password, device_params):
    return manager.connect(host=host,
                           port=port,
                           username=user,
                           password=password,
                           device_params=device_params,
                           hostkey_verify-false)


def enable_nc_prefix(host, user, password):
    # add a parameter 'with_ns' to turn on/off 'nc'
    device_params = {'name': 'ericsson', 'with_ns': True}
    with ericsson_connect(host,
                          port=22,
                          user=user,
                          password=password,
                          device_params=device_params) as m:

        ret = m.get_config(source="running").data_xml
        print(ret)


def disable_nc_prefix(host, user, password):
    # add a parameter 'with_ns' to turn on/off 'nc'
    device_params = {'name': 'ericsson', 'with_ns': False}
    with ericsson_connect(host,
                          port=22,
                          user=user,
                          password=password,
                          device_params=device_params) as m:

        ret = m.get_config(source="running").data_xml
        print(ret)


def demo(host, user, password):
    enable_nc_prefix(host, user, password)
    print("#"*50)
    disable_nc_prefix(host, user, password)


if __name__ == '__main__':
    demo(sys.argv[1], sys.argv[2], sys.argv[3])