-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathparamiko_get_nxos_int_json.py~
86 lines (61 loc) · 2.39 KB
/
paramiko_get_nxos_int_json.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
import paramiko
import optparse
import time
import json
import sys
def disable_paging(remote_conn):
'''Disable paging on a Cisco router'''
remote_conn.send("terminal length 0\n")
time.sleep(1)
# Clear the buffer on the screen
output = remote_conn.recv(1000)
return output
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option('-i', '--ip',dest="ip",
help="[Mandatory] NXOS IP Address")
parser.add_option('-u', '--username',dest="userName",
help="[Mandatory] Account Username for NXOS Login")
parser.add_option('-p', '--password',dest="password",
help="[Mandatory] Account Password for NXOS Login")
(options, args) = parser.parse_args()
if not options.ip:
parser.print_help()
parser.error("Provide NXOS IP Address")
if not options.userName:
parser.print_help()
parser.error("Provide NXOS UserName")
if not options.password:
options.password=getpassword("NXOS Password:")
# Create instance of SSHClient object
remote_conn_pre = paramiko.SSHClient()
# Automatically add untrusted hosts (make sure okay for security policy in your environment)
remote_conn_pre.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
# initiate SSH connection
remote_conn_pre.connect(options.ip, username=options.userName, password=options.password)
print "--- SSH connection established to %s ---" % options.ip
# Use invoke_shell to establish an 'interactive session'
remote_conn = remote_conn_pre.invoke_shell()
print "--- Interactive SSH session established ---"
# Strip the initial router prompt
output = remote_conn.recv(1000)
# See what we have
#print output
# Turn off paging
disable_paging(remote_conn)
# Now let's try to send the router a command
remote_conn.send("\n")
remote_conn.send("show int brief | json \n")
# Wait for the command to complete
time.sleep(2)
output = remote_conn.recv(50000)
#print 'output', output
# Get second line which is the json output
output = output.split("\n")[2]
int_brief = json.loads(output)
print "--- INTERFACES that are UP --- "
int_list = int_brief['TABLE_interface']['ROW_interface']
for int in int_list:
if(int['state'] == "up"):
print int['interface']