-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.py
More file actions
executable file
·378 lines (339 loc) · 16.7 KB
/
cli.py
File metadata and controls
executable file
·378 lines (339 loc) · 16.7 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/env python3
# Copyright 2016 Netfishers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import pprint
import sys
import os
import hashlib
from libs import constants
from libs.connection import BuildCommand
from libs.connection import Connection
from libs.connection import Login
from libs.connection import SendCommand
from libs.connection import SendPassword
from libs.cryptolib import decrypt_file_to_array
from libs.getpass import getpass
from libs.utils import BuildLogfile
from libs.utils import CleanComments
def main():
p = argparse.ArgumentParser(
usage='''
%(prog)s -r "remote host"| -f "list of hosts" [options]
%(prog)s -h #get help on numerous options
''' % { "prog": sys.argv[0]},
description='''
A small tool to log into any piece of equipment with a decent CLI interface...
\nYou can pass a lot of options to comply with most of devices behavior.
\n\n
Note that if you ask %(prog)s to look for options in a file, that is "-o" option,
this file obviously contains your credentials, passwords,
and thus MUST be encrypted using the "cipher.py" tool.
\n\nEnjoy !
''' % { "prog": sys.argv[0]},
conflict_handler='resolve'
)
mut_excl_1 = p.add_mutually_exclusive_group()
mut_excl_1.add_argument('-f', '--hosts-file', action='store', type=str, dest='array', metavar='FILE',
help='file containing a list of hosts')
mut_excl_1.add_argument('-r', '--remote-host', action='store', type=str, dest='remote', metavar='REMOTE_HOSTNAME',
help='remote host name or IP')
mut_excl_2 = p.add_mutually_exclusive_group()
mut_excl_2.add_argument('-c', '--command', action='store', type=str, dest='cmd', nargs='+', metavar='COMMAND',
help='command(s) to be executed on remote host')
mut_excl_2.add_argument('-cf', '--command-file', action='store', type=str, dest='cmdfile', metavar='COMMAND_FILE',
help='file containing a list of commands')
mut_excl_3 = p.add_mutually_exclusive_group()
mut_excl_3.add_argument('-i', '--interact', action='store_true', dest='interact',
help='human interaction after connection setup')
mut_excl_3.add_argument('-l', '--logfile', action='store_true', dest='logfile',
help='create a logging file in "logs" subdirectory')
p.add_argument('-d', '--debug', action='store_true', dest='debug',
help='debug mode will out in clear all arguments passed to the script')
p.add_argument('-e', '--exitcommand', action='store', type=str, dest='exitcommand', metavar='EXIT_COMMAND',
help='command used to exit from a remote host')
p.add_argument('-j', '--jumphost-credentials', action='store', type=str, dest='jumphost', metavar='LIST', nargs='+',
help='an ordered list: (protocol,host,port,username,password,prompt,timeout,verbose)\nyou can omit latest elements')
p.add_argument('-m', '--no-more', action='store', type=str, dest='more',
help='command to pass to remote host to avoid --more-- in execution, defaults to "terminal length 0"')
p.add_argument('-o', '--options-override', action='store', type=str, dest='override',
help='ciphered file containing arguments that override command line options')
p.add_argument('-p', '--prompt', action='store', type=str, dest='prompt',
help='expected prompt from remote host, this field accepts quoted regex')
p.add_argument('-po', '--port-number', action='store', type=str, dest='port', metavar='TCP_PORT_NUMBER',
help='TCP port number for connection')
p.add_argument('-s', '--sub-proc', action='store', type=str, dest='sub', nargs=2, metavar=('MODULE', 'METHOD'),
help='module and function to execute after the connection is established')
p.add_argument('-ss', '--presub-proc', action='store', type=str, dest='presub', nargs=2, metavar=('MODULE', 'METHOD'),
help='module and function to execute before connection to remote device')
p.add_argument('-t', '--timeout', action='store', type=int, dest='timeout',
help='max seconds to wait for an answer from remote host')
p.add_argument('-u', '--username-credentials', action='store', type=str, dest='user', metavar=('USERNAME'),
help='username to log into remote host')
p.add_argument('-v', '--verbose', action='store_true', dest='verbose',
help='unhide connection process, useful for debugging')
p.add_argument('-w', '--password', action='store', type=str, dest='password', nargs='+', metavar=('PASSWORD'),
help='password to log into remote host and optionally an enable password')
p.add_argument('-x', '--protocol', action='store', type=str, dest='proto', choices=['telnet', 'ssh'],
help='protocol to be used for connection, defaults to ' + constants.DEFAULT_PROTOCOL)
p.set_defaults(
debug=False,
exitcommand=constants.EXIT_COMMAND,
interact=False,
logfile=False,
more=constants.MORE,
prompt=constants.PROMPT,
proto=constants.DEFAULT_PROTOCOL,
timeout='15',
user='',
verbose=False
)
# parse arguments from command line
args = p.parse_args()
# if "o" options is set,override arguments from local encrypted file
if args.override:
if not os.path.isfile(args.override):exit('ERROR: Invalid filename: "' + args.override + '"')
else:
password = getpass('Please enter your password: ')
password_encoded = password.encode('utf-8')
key = hashlib.sha256(password_encoded).digest()
try:
args_override = decrypt_file_to_array(key, args.override)
except ValueError:
exit('ERROR: The file ' + args.override + ' does not seem to be encrypted...')
args_override_cleaned = CleanComments(args_override)
if len(args_override_cleaned) == 0:
exit('ERROR: wrong password, or file was empty...')
else:
try:
# Vérifier que la première ligne est une str valide ASCII
if not isinstance(args_override_cleaned[0], str):
raise UnicodeDecodeError("Premier élément n'est pas une string")
args_override_cleaned[0].encode('ascii') # Test encodage ASCII
# and now merge arguments in initial namespace
p.parse_args(args_override_cleaned, namespace=args)
except (UnicodeDecodeError, UnicodeEncodeError):
exit('ERROR: wrong password or invalid file format...')
#import presub procedure if any
if args.presub:
import importlib
try:
mod = importlib.import_module(args.presub[0])
presubmethod = getattr(mod, args.presub[1])
except ImportError as e:exit(e)
# if debug mode is on, we start with araw output of args
if args.debug:
print('debug mode is on')
pprint.pprint(vars(args))
# we need at least one of them
if (args.remote is None and args.array is None):
p.error(' remote host IP address or name is required,\nyou miss either "-r" or "-f" option\n\n')
# here below, we try to get a host list from user arguments so we can iterate over this afterwards
if args.remote:
listhosts_cleaned = [args.remote]
elif args.array:
try:
f = open(args.array)
listhosts = f.read().splitlines()
listhosts_cleaned = CleanComments(listhosts)
#remove leading and trailing unexpected characters
listhosts_cleaned = [i.strip() for i in listhosts_cleaned]
#args.interact = False
except ValueError:
print("WARNING: cannot open " + args.array + " not a usable file..")
if args.debug:
print('List of hosts:')
pprint.pprint(listhosts_cleaned)
# if cmdfile or commands are provided, we build a list and sanitize it
if args.cmdfile:
try:
f = open(args.cmdfile)
listcmd = f.read().splitlines()
listcmd_cleaned = CleanComments(listcmd)
#remove leading and trailing unexpected characters
listcmd_cleaned = [i.strip() for i in listcmd_cleaned]
except ValueError:
print("ERROR: cannot open " + args.cmdfile + " not a usable file..")
elif args.cmd:
listcmd_cleaned = CleanComments(args.cmd)
if args.debug and (args.cmdfile or args.cmd):
print('List of commands:')
pprint.pprint(listcmd_cleaned)
# empty default port number if telnet without port options
if args.proto == 'telnet' and not args.port:args.port = ''
# defaults to port number 22 if ssh without port options
if args.proto == 'ssh' and not args.port:args.port = '22'
# complete password array with empty strings
if args.password:
password = args.password[0]
try:enablepassword = args.password[1]
except:
args.password.append('')
enablepassword = ''
else:
password = ''
enablepassword = ''
# complete jumphost credentials array with empty strings and initiate connection
if args.jumphost:
try:args.jumphost[0]
except:exit('ERROR: if "-j" option is set we need at least protocol and jumphost address')
try:args.jumphost[1]
except:exit('ERROR: if "-j" option is set we need at least protocol and jumphost address')
try:
args.jumphost[2]
try:int(args.jumphost[2])
except:exit('ERROR: "args.jumphost[2]" is not a valid port number')
except:
if args.jumphost[1] == 'ssh':args.jumphost.append('22')
elif args.jumphost[1] == 'telnet':args.jumphost.append('')
try:args.jumphost[3]
except:args.jumphost.append('')
try:args.jumphost[4]
except:args.jumphost.append('')
try:args.jumphost[5]
except:args.jumphost.append('')
c = Connection(*(args.jumphost + [args.timeout] + [args.verbose]))
if c == False:exit('ERROR: Cannot connect to jumphost')
# execute presub method if any
if args.presub and presubmethod:
try:presubmethod(args,c)
except (ImportError, AttributeError, NameError) as e:exit(e)
else:
# execute presub method if any and no jumphost (no c object referenced at this point)
if args.presub and presubmethod:
try:presubmethod(args)
except (ImportError, AttributeError, NameError) as e:exit(e)
# import sub procedure if any
if args.sub:
import importlib
try:
mod = importlib.import_module(args.sub[0])
submethod = getattr(mod, args.sub[1])
except ImportError as e:exit(e)
# main loop through hosts
for host in listhosts_cleaned:
h = host.rstrip('\n')
# in case we loop through an array, we want the actual remote host to be part of 'args' object
if args.array:args.remote = h
if not args.jumphost:
c = Connection(
args.proto, h, args.port, args.user, password,
args.prompt, args.timeout, args.verbose
)
if c == False:
print('ERROR: Cannot connect to remote host: ' + h)
continue
else:
cmd = BuildCommand(args.proto, h, args.port, args.user)
c.sendline(cmd)
l = Login(c, args.proto, h, args.port, args.user, password,
args.prompt, args.timeout, args.verbose
)
if l == False:
print('ERROR: Cannot connect to remote host: ' + h)
continue
# now switch to enable mode if needed
if len(enablepassword) > 0:
c.sendline('enable')
index = c.expect(['assword:',args.prompt])
if index == 0:
if SendPassword(enablepassword, args.prompt, c, args.timeout):
print("\n>>> connected to: " + h)
elif index == 1:
print('INFO: enable password not needed for remote host: ' + h)
# print internal details of pexpect object when debug option is turned on
if args.debug:print('\n\nPexpect object details:\n\n'+str(c))
# create a logfile if asked to do so
if args.logfile:
logfile = BuildLogfile(h)
try:
fout = open(logfile, 'wb')
except (IOError, OSError) as e:
print('WARNING; cannot log output because logfile cannot be opened...')
print ("I/O error({0}): {1}").format(e.errno, e.strerror)
# if commands in args or in a cmdfile, then prepare terminal length
if args.cmdfile or args.cmd:
SendCommand(c, args.more, args.prompt, args.timeout)
print("\t>>> now executing commands from " + str(listcmd_cleaned) + " on " + h)
if not args.logfile:c.logfile_read = sys.stdout
# loop through the commands
for line in listcmd_cleaned:
#print("\t>>> "+line)
SendCommand(c, line, args.prompt, args.timeout)
# if logfile is set, we send a clean output inside the loop
if args.logfile:
try:
fout.write(b'\n\n----- '+line.encode()+b' -----\n')
fout.write(c.before.encode())
except (IOError, OSError):print('WARNING: cannot log output to ' + args.logfile)
else:
if args.logfile:
c.logfile = fout
c.logfile_send = None
print("\t>>> now logging output from " + h + " in " + logfile)
# execute sub method
if args.sub:
try:submethod(args,c)
except (ImportError, AttributeError, NameError) as e:exit(e)
# pass in interact mode, hit escape character to end connection
if args.interact is True:
SendCommand(c, '\n', args.prompt, args.timeout)
# Display the current prompt before entering interactive mode
sys.stdout.write(c.after.decode() if isinstance(c.after, bytes) else c.after)
sys.stdout.flush()
logfile = None
fout = None
logging_enabled = False
while True:
if logging_enabled and fout is not None:
c.logfile = fout
c.logfile_read = fout
c.logfile_send = None
else:
c.logfile = None
c.logfile_read = None
c.logfile_send = None
c.interact(constants.ESCAPE_CHARACTER)
choice = input("(cli-wrapper) [l]og on/off, [q]uit, [c]ontinue ? ").strip().lower()
if choice == 'l':
if fout is None:
# création à la demande
logfile = BuildLogfile(h)
try:
fout = open(logfile, 'wb')
logging_enabled = True
print(">>> logging started for host %s in %s" % (h, logfile))
except (IOError, OSError) as e:
print(">>> cannot open logfile for host %s: %s" % (h, e))
fout = None
logging_enabled = False
else:
# toggle sur fichier existant
logging_enabled = not logging_enabled
state = "ON" if logging_enabled else "OFF"
print(">>> logging is now %s for host %s" % (state, h))
continue
elif choice == 'c':
continue
elif choice == 'q':
break
print('\n<<< gracefully exited from: ' + h + '\n')
if fout is not None:
fout.close()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print ('\n!!! program interrupted !!!\n')
sys.exit(0)