-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNSConnect.py
More file actions
executable file
·329 lines (282 loc) · 12.1 KB
/
NSConnect.py
File metadata and controls
executable file
·329 lines (282 loc) · 12.1 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
"""
NSConnect.py
Net Scout simple CLI script to connect and disconnect ports based on their
port names. After single execution will store connection and login info into
a settings file that is not secure. It is encoded to provide minimal security.
This module can be added to provide much more functionality as requested.
Execute NetScout command
optional arguments:
-h, --help show this help message and exit
--connect port1 port2
Create a connection between two ports
--disconnect port [port ...]
Disconnect a port(s) from its connection
Copyright 2016 Christian Trautman ctrautma@redhat.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import argparse
import base64
import os
import sys
if sys.version_info[0] == 2:
import ConfigParser as configparser
else:
import configparser
import locale
import telnetliblog
from time import sleep
_LOCALE = locale.getlocale()[1]
class NetScout_Command(object):
def __init__(self, parser, args):
self.args = args
if not any([args.connect,
args.disconnect,
args.downloadhelp,
args.listports,
args.listgroups,
args.portinfo,
args.resetconfig,
args.isconnected,
args.showconnections]):
print("No actions provided...")
parser.print_help()
sys.exit(1)
self._cfg = configparser.ConfigParser()
if self.args.resetconfig:
self.resetconfig()
print("Checking for config file...")
self._cfg.read('settings.cfg')
try:
self._ip_addr = base64.b64decode(
self._cfg['INFO']['host']).decode(_LOCALE)
self._port = base64.b64decode(
self._cfg['INFO']['port']).decode(_LOCALE)
self._username = base64.b64decode(
self._cfg['INFO']['username']).decode(_LOCALE)
self._password = base64.b64decode(
self._cfg['INFO']['password']).decode(_LOCALE)
print("Config file found and read...")
except KeyError:
self.write_settings()
print("Connecting to Netscout at {}".format(self._ip_addr))
self.tn = telnetliblog.Telnet2(self._ip_addr, self._port)
try:
self.logon()
except Exception as e:
print(e)
import sys
sys.exit()
if self.args.downloadhelp:
self.downloadhelp()
self.model = self.get_switch_model()
self.parse_args()
def connect(self, ports):
if self.model == "HS-3200":
self.connect_hs3200(ports)
else:
self.issue_command('connect PORT {} to PORT {} force'.format(*ports))
def connect_hs3200(self, ports):
substr = "ERROR"
port_compatible= "Port interfaces are not compatible!"
print('Connecting ports {} {}'.format(*ports))
port_list = self.list_ports_internal()
for i in ports:
if not i in port_list:
print("INPUT PORT invalid ,please check")
return
#First disconnet all ports
print("First disconnect all ports that connected")
self.disconnect_hs3200(ports)
conn_out = self.get_command_output('connect -i -d PORT {} PORT {}'.format(*ports))
if port_compatible in str(conn_out):
print(port_compatible)
return
out = self.get_command_output('activate -d PORT {} PORT {}'.format(*ports))
out = out.decode(_LOCALE).split('\r\n')
for line in out[0:-1]:
if substr in line:
self.disconnect_hs3200(ports)
self.connect_hs3200(ports)
break
print('Connecting ports {} {} Done OK'.format(*ports))
pass
def disconnect(self, ports):
if self.model == "HS-3200":
self.disconnect_hs3200(ports)
else:
for port in ports:
self.issue_command('connect PORT {} to null force'.format(port))
def disconnect_hs3200(self, ports):
print('Disconnecting connections to ports {}'.format(" ".join(ports)))
for port in ports:
self.issue_command(f'disconnect -i -d -F PORT {port} PORT *')
def getalltopo(self):
out = self.get_command_output('show topo all', timeout=60)
out = out.decode(_LOCALE).split('\r\n')
return [ " ".join(i.split(" ")[0:-1]).rstrip(" ") for i in out[2:-2]]
def isconnected(self, port):
connect_status=0
connected_ports = self.getconnected(port[0])
if None != connected_ports:
print(connected_ports)
return True
else:
return False
def getconnected(self, port):
out = self.get_command_output('show conn ports', timeout=60)
out = out.decode(_LOCALE).split('\r\n')
for line in out[0:-1]:
if port in line:
return line
return None
def get_switch_name(self):
out = self.get_command_output('show switch', timeout=60)
out = out.decode(_LOCALE).split('\r\n')
switch_name = out[0:-1][2]
return switch_name
def get_switch_model(self):
substr = "Model"
switch_model=""
switch_name = self.get_switch_name()
out = self.get_command_output('show switch {}'.format(switch_name), timeout=60)
out = out.decode(_LOCALE).split('\r\n')
for line in out[0:-1]:
if substr in line:
switch_model = line.split()[3][:-1]
break
return switch_model
def downloadhelp(self):
self.tn.write('\r'.encode(_LOCALE))
out = self.tn.expect([r'=>'.encode(_LOCALE)], timeout=30)
self.tn.write('help\r'.encode(_LOCALE))
print("Downloading help file to helpfile.txt")
out = self.tn.expect([r'=>'.encode(_LOCALE)], timeout=300, decoding='windows-1252')
with open('helpfile.txt', 'w') as fh:
fh.writelines(str(out[2]))
sys.exit(0)
def issue_command(self, cmd, timeout=30):
self.tn.write('{}\r'.format(cmd).encode(_LOCALE))
out = self.tn.expect([r'=>'.encode(_LOCALE)], timeout=timeout)
if out[0] != -1:
return True
else:
return False
def get_command_output(self, cmd, timeout=30):
self.tn.write('{}\r'.format(cmd).encode(_LOCALE))
out = self.tn.expect([r'=>'.encode(_LOCALE)], timeout=timeout)
if out[0] != -1:
return out[2]
else:
return ''
def list_groups(self):
out = self.get_command_output('show groups', timeout=60)
out = out.decode(_LOCALE).split('\r\n')
for line in out[2:-2]:
print(line)
def list_ports_internal(self):
out = self.get_command_output('show ports', timeout=60)
out = out.decode(_LOCALE).split('\r\n')
return out[2:-2]
def list_ports(self):
out = self.list_ports_internal()
for line in out:
print(line)
def logon(self):
print("Attempting to logon to Netscout...")
self.tn.write('\r'.encode(_LOCALE))
out = self.tn.expect([r'=>'.encode(_LOCALE)], timeout=30)
if out[0] != -1:
self.tn.write('logon {}\r'.format(self._username).encode(_LOCALE))
else:
raise RuntimeError('Failed to get basic prompt on telnet!!!')
out = self.tn.expect(['Password:'.encode(_LOCALE)], timeout=30)
sleep(1)
if out[0] != -1:
self.tn.write('{}\r'.format(self._password).encode(_LOCALE))
else:
raise RuntimeError('Did not get password prompt!!!')
out = self.tn.expect([r'=>'.encode(_LOCALE)], timeout=30)
if str(out[2]).find('Access denied. Username/Password is invalid!') > -1:
raise Exception('Access denied. Username/Password is invalid!')
if out[0] == -1:
raise RuntimeError('Failed to logon!!!')
def parse_args(self):
if self.args.connect:
self.connect(self.args.connect)
if self.args.disconnect:
self.disconnect(self.args.disconnect)
if self.args.listports:
self.list_ports()
if self.args.listgroups:
self.list_groups()
if self.args.portinfo:
self.show_port_info(self.args.portinfo)
if self.args.showconnections:
self.show_port_connections()
if self.args.isconnected:
self.isconnected(self.args.isconnected)
def resetconfig(self):
if os.path.exists('settings.cfg'):
os.remove('settings.cfg')
def show_port_connections(self):
out = self.get_command_output('show connected ports', timeout=60)
out = out.decode(_LOCALE).split('\r\n')
for line in out[2:-2]:
print(line)
def show_port_info(self, ports):
for port in ports:
out = self.get_command_output(
'show information Port {}'.format(port), timeout=60)
out = out.decode(_LOCALE).split('\r\n')
for line in out[0:-1]:
print(line)
def write_settings(self):
print("Config file not present....")
print("Please answer the following questions.")
self._ip_addr = input("Netscout IP address:")
self._port = input("Netscout telnet port:")
self._username = input("Netscout username:")
from getpass import getpass
self._password = getpass("Netscout password:")
self._cfg['INFO'] = {
'host': base64.b64encode(self._ip_addr.encode(_LOCALE)).decode(),
'port': base64.b64encode(self._port.encode(_LOCALE)).decode(),
'username': base64.b64encode(
self._username.encode(_LOCALE)).decode(),
'password': base64.b64encode(
self._password.encode(_LOCALE)).decode()}
with open('settings.cfg', 'w') as fh:
self._cfg.write(fh)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Execute NetScout command')
parser.add_argument('--connect', nargs=2, type=str,
help='Create a connection between two ports',
required=False, metavar=('port1', 'port2'))
parser.add_argument('--disconnect', nargs='+', type=str,
help='Disconnect a port(s) from its connection',
required=False, metavar=('port', 'port'))
parser.add_argument('--downloadhelp', action='store_true',
help='download help to local file and exit', required=False)
parser.add_argument('--listgroups', action='store_true',
help='Show list of available groups', required=False)
parser.add_argument('--listports', action='store_true',
help='Show list of available ports', required=False)
parser.add_argument('--portinfo', nargs='+', type=str,
help='Show information on ports', required=False)
parser.add_argument('--resetconfig', action='store_true',
help='Reset config file', required=False)
parser.add_argument('--showconnections', action='store_true',
help='Show active port connections', required=False)
parser.add_argument('--isconnected', nargs='+', type=str,
help='Returns 1 if port is connected. Need port as an argument.', required=False)
args = parser.parse_args()
NETS = NetScout_Command(parser, args)