Debian Codegraph @

examples/vendor/nokia/sros/md_cli_raw_command.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/env python
2 #
3 # For Nokia SR OS > 20.10, support for issuing a subset of CLI commands
4 # (e.g. 'show', 'admin', 'clear', 'ping', etc..) over NETCONF RPCs is
5 # achieved by the use of the <md-cli-raw-command> YANG 1.1 action
6
7 import logging
8 import sys
9
10 from ncclient import manager
11 from ncclient.operations.rpc import RPCError
12
13 _NS_MAP = {
14 'nc': 'urn:ietf:params:xml:ns:netconf:base:1.0',
15 'nokia-oper': 'urn:nokia.com:sros:ns:yang:sr:oper-global'
16 }
17
18 def connect(host, port, user, password):
19 m = manager.connect(host=host, port=port,
20 username=user, password=password,
21 device_params={'name': 'sros'},
22 hostkey_verify=False)
23
24 ## Issue 'show card' and display the raw XML output
25 result = m.md_cli_raw_command('show card')
26 logging.info(result)
27
28 ## Issue 'show port' from MD-CLI context and emit only
29 ## the returned command contents
30 result = m.md_cli_raw_command('show port')
31 output = result.xpath(
32 '/nc:rpc-reply/nokia-oper:results' \
33 '/nokia-oper:md-cli-output-block',
34 namespaces=_NS_MAP)[0].text
35 logging.info(output)
36
37 ## Issue 'show version' from Classic CLI context and emit
38 ## only the returned command contents
39 result = m.md_cli_raw_command('//show version')
40 output = result.xpath(
41 '/nc:rpc-reply/nokia-oper:results' \
42 '/nokia-oper:md-cli-output-block',
43 namespaces=_NS_MAP)[0].text
44 logging.info(output)
45
46 ## Issue 'ping' from MD-CLI context and emit only the
47 ## returned command contents
48 result = m.md_cli_raw_command(
49 'ping 127.0.0.1 router-instance "Base" count 3')
50 output = result.xpath(
51 '/nc:rpc-reply/nokia-oper:results' \
52 '/nokia-oper:md-cli-output-block',
53 namespaces=_NS_MAP)[0].text
54 logging.info(output)
55
56 ## Issue an admin command that returns only NETCONF <ok/> or
57 ## <rpc-error/>
58 result = m.md_cli_raw_command('admin save')
59 try:
60 if len(result.xpath('/nc:rpc-reply/nc:ok', namespaces=_NS_MAP)) > 0:
61 logging.info('Admin save successful')
62 else:
63 logging.info('Admin save unsuccessful')
64
65 except RPCError as err:
66 logging.info('Error: %s' % err.message.strip())
67
68
69 ## Issue an unsupported command and handle the RPC error gracefully
70 try:
71 result = m.md_cli_raw_command('configure')
72 if len(result.xpath('/nc:rpc-reply/nc:ok', namespaces=_NS_MAP)) > 0:
73 logging.info('Command successful')
74 else:
75 logging.info('Command unsuccessful')
76
77 except RPCError as err:
78 logging.info('Error: %s' % err.message.strip())
79
80 m.close_session()
81
82 if __name__ == '__main__':
83 LOG_FORMAT = '%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s'
84 logging.basicConfig(stream=sys.stdout, level=logging.INFO, format=LOG_FORMAT)
85
86 try:
87 connect(sys.argv[1], '830', 'admin', 'admin')
88
89 except IndexError:
90 logging.error('Must supply a valid hostname or IP address')
91
92