File: get_config.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 (55 lines) | stat: -rw-r--r-- 1,707 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
#!/usr/bin/python3

import logging

from lxml import etree
from ncclient import manager
from ncclient.xml_ import *


def connect(host, port, user, password):
    conn = manager.connect(host=host,
                           port=port,
                           username=user,
                           password=password,
                           timeout=10,
                           device_params={'name': 'alu'},
                           hostkey_verify=False)


    logging.info('Retrieving full config, please wait ...')

    result = conn.get_configuration()
    logging.info(result)

    logging.info('Here is the chassis configuration')
    output = result.xpath('data/configure/system/chassis')[0]
    logging.info(to_xml(output))

    logging.info('Retrieving service config')

    # specify filter to pass to get_config
    filter = new_ele('configure', attrs={'xmlns': ALU_CONFIG})
    sub_ele(filter, 'service')
    result = conn.get_configuration(filter=filter)
    epipes = result.xpath('data/configure/service/epipe')

    for i in epipes:
        logging.info(etree.tostring(i, pretty_print=True).decode('utf-8'))

    logging.info('Getting CLI -config')
    cli_cfg = conn.get_configuration(content='cli', filter=['port 1/1/11'])
    logging.info(cli_cfg)

    logging.info('Get detailed CLI -config')
    cli_cfg = conn.get_configuration(content='cli', filter=['port 1/1/11'], detail=True)
    logging.info(cli_cfg)

    conn.close_session()


if __name__ == '__main__':
    LOG_FORMAT = '%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s'
    logging.basicConfig(stream=sys.stdout, level=logging.INFO, format=LOG_FORMAT)

    connect('localhost', 830, 'admin', 'admin')