-
Notifications
You must be signed in to change notification settings - Fork 16
/
get_netconf_CLI_HPN.py
90 lines (71 loc) · 3.03 KB
/
get_netconf_CLI_HPN.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
'''
This is simple script to execute CLI commands from HPN switch or router via NETCONF over SSH.
This program can be easily extended to meet your own requirements. I have developed the program with
HP Network simulator. When this program is used against physical HP Comware Network switches
please change h3c.com in hp.com.
'''
__author__ = "Dobias van Ingen"
__credits__ = "Jason Edelman for insipring me in creating this program (http://www.jedelman.com/)"
__license__ = "MIT"
__version__ = "0.3"
__maintainer__ = "Dobias van Ingen"
__email__ = "dobias.vaningen@gmail.com"
from ncclient import manager
import argparse
import xml.dom.minidom
'''
This section you can provide multiple NETCONF code that will be executed
on the device
'''
display_command = """
<rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<CLI>
<Execution> %s </Execution>
</CLI>
</rpc>
"""
def exec_command(connection, command):
# Execute command on device
code_snip = display_command % command
edit_response = connection.dispatch(code_snip)
return edit_response
def connect_device(hostname, username, password):
# Set-up NETCONF over SSH session
netconf_manager = manager.connect(host=hostname,
port=830,
username=username,
password=password,
hostkey_verify=False,
allow_agent=False,
look_for_keys=False
)
return netconf_manager
def disconnect_device(connection):
# Close NETCONF session and transport
connection.close_session()
def main():
DEBUG = True
# Argument parsing
parser = argparse.ArgumentParser(description="Execute CLI commands via NETCONF on HP switch / router.")
parser.add_argument("--host", help="Hostname or IP address which to connect", action="store", dest="hostname", type=str)
parser.add_argument("--usr", help="Username of device", action="store", dest="username", type=str)
parser.add_argument("--pass", help="Password of device", action="store", dest="password", type=str)
parser.add_argument("--cmd", help="Command to execute on device", action="store", dest="command", type=str)
cli_args = parser.parse_args()
hostname = cli_args.hostname
username = cli_args.username
password = cli_args.password
command = cli_args.command
# Create connection to device
if DEBUG: print"Set-up connection: {}\n".format(hostname)
netconf_manager = connect_device(hostname, username, password)
# Execute CLI command on device
if DEBUG: print"Execute following command: {}".format(command)
command_resp = exec_command(netconf_manager, command)
if DEBUG: print"*** NETCONF response ***\n {}\n".format(command_resp)
# Disconnect connection
if DEBUG: print"Disconnect from: {}\n".format(hostname)
disconnect_device(netconf_manager)
if __name__ == "__main__":
main()