-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-get-oper.py
157 lines (124 loc) · 5.1 KB
/
simple-get-oper.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
import paramiko
import sys
import socket
from time import sleep
from lxml import etree
#### Exit Codes
# -1 = missing all arguments
# 1 = ip not in right format
# 2 = missing number of iterations or it' not an integer
# 3 = missing sleep time
def verify_arguments(arguments):
# This function verifies that we have proper number of arguments
# also verifies ip is in proper format
if len(arguments) < 4:
print """Please use script as follows:
<script> <ip> <username> <password> <Operation> <iterations> <sleep time in minutes>
"""
sys.exit(-1)
if not socket.inet_aton(arguments[1]):
print 'IP is not in right format'
sys.exit(1)
if not int(sys.argv[5]):
print 'Please include number of iterations'
sys.exit(2)
if not float(sys.argv[6]):
print 'Please include sleep time between queries in seconds'
sys.exit(3)
def connect_paramiko(router_ip_p, username_p, password_p):
# This function creates an object by connecting to a router passed by arguments
# it then returns the object
router = paramiko.SSHClient()
router.load_system_host_keys()
router.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
router.connect(router_ip_p, username=username_p, password=password_p, allow_agent=False, look_for_keys=False, timeout=None)
print 'Connected via SSH to {}'.format(router_ip_p)
except:
print 'Couldn\'t connect to {}'.format(router_ip_p)
return router
def start_netconf(self):
# This function sends a "netconf format" to the router object
# It then returns the stdin, stdout, stderr output back
stdin, stdout, stderr = self.exec_command('netconf format', get_pty=True)
return stdin, stdout, stderr
def get_oper_netconf(self, operation, stdin, stdout, stderr):
# This function will get operational data from a router object
# operation = i.e. 'InterfaceTable'
supported_operations = ['InterfaceTable']
hello = """
<?xml version="1.0" encoding="UTF-8" ?>
<hello>
<capabilities>
<capability>
urn:ietf:params:netconf:base:1.0
</capability>
</capabilities>
</hello>
]]>]]>
"""
close = """
<?xml version="1.0" encoding="UTF-8" ?>
<rpc message-id="1" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<close-session/>
</rpc>
]]>]]>
"""
# First verify it's a supported operation for this script
if operation in supported_operations:
print 'Operation is supported'
if operation == 'InterfaceTable':
print 'InterfaceTable requested'
query_interfaceTable="""<?xml version="1.0" encoding="UTF-8" ?>
<rpc message-id="1" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<get>
<filter>
<Operational>
<Interfaces>
<InterfaceTable>
</InterfaceTable>
</Interfaces>
</Operational>
</filter>
</get>
</rpc>
]]>]]>"""
header = """<?xml version="1.0" encoding="UTF-8" ?>
<rpc message-id="1" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">"""
ending = """</rpc>
]]>]]>"""
# Now we loop through the number of iterations mentioned.
stdin.write(hello + '\n')
stdin.write(query_interfaceTable + '\n')
stdin.flush()
# the loop below will stop until we see rpc-reply tag closing, which means we finish the reply
# print.
for line in stdout:
print line.rstrip()
if line.strip() == '</rpc-reply>':
break
def main():
# first verify arguments
verify_arguments(sys.argv)
# assign variables
router_ip = sys.argv[1]
username = sys.argv[2]
password = sys.argv[3]
operation = sys.argv[4]
num_iterations = sys.argv[5]
sleep_time = sys.argv[6]
count=0
# We will loop through the whole process depending on the number of iterations requested.
while int(count) < int(num_iterations):
# now we need to connect to the router in question
router = connect_paramiko(router_ip, username, password)
# now need to start netconf shell
stdin, stdout, stderr = start_netconf(router)
# now we can actually send a get via netconf along with hello
get_oper_netconf(router, operation, stdin, stdout, stderr)
router.close()
count += 1
sleep(float(sleep_time))
if __name__ == '__main__':
main()