-
Notifications
You must be signed in to change notification settings - Fork 1
/
teletel.py
95 lines (78 loc) · 3.46 KB
/
teletel.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
mport telnetlib, re, os, sys
from optparse import OptionParser
def get_servers(file):
#Need to add some code here
return servers
def get_cmd(list):
#Need to add some code here
return cmds
def do_log(data):
#Need to add some code here
return
def send_cmd(cmd):
global tn
if opts.verbose:
if opts.quiet == False:
print("Sending command " + cmd)
tn.write(cmd + "\r\n")
output = tn.read_until("\n# ")
ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
return ansi_escape.sub('', output)
def tel_login():
global tn
if opts.verbose:
if not opts.quiet:
pass
print("Connecting to "+s)
tn.read_until("login: ")
tn.write(opts.username + "\r\n")
tn.read_until("Password:")
tn.write(opts.password + "\r\n")
tn.read_until("\n# ")
return
def get_opts():
parse = OptionParser()
parse.add_option("-s", "--serverlist", dest="serverlist", metavar="FILE", help="File containing list of servers to run commands on.")
parse.add_option("-m", "--machine", dest="server", metavar="MACHINE", action="append", help="Run command(s) on one machine [MACHINE].")
parse.add_option("-c", "--commandlist", dest="cmdlist", metavar="FILE", help="File containing a list of commands to run.")
parse.add_option("-x", "--execute", dest="command", metavar="COMMAND", action="append", help="Execute single command COMMAND only.")
parse.add_option("-o", "--output", dest="outfile", metavar="OUTFILE", help="Specify an option output file.")
parse.add_option("--parallel", dest="parallel", default="False", action="store_true", help="Use Multithreading and run commands in parallel.")
parse.add_option("-v", dest="verbose", action="store_true", help="Be verbose about what is going on.")
parse.add_option("-u", "--user", dest="username", metavar="USERNAME", help="Specify a username to login to server(s).")
parse.add_option("-p", "--password", dest="password", metavar="PASSWORD", help="Specify a password to login to server(s).")
parse.add_option("-q", "--quiet", dest="quiet", action="store_true", help="Don't display any output.")
(options, args) = parse.parse_args()
if options.serverlist and options.server:
parse.error("Must specify either single machine [-m MACHINE] or file with list of machines [-s FILE].")
if options.command and options.cmdlist:
parse.error("Must specify either a single command [-x COMMAND] or file with list of commands [-c FILE].")
if options.parallel == "True":
parse.error("Sorry. This isn't supported yet.")
if not options.username or not options.password:
parse.error("You must specify both a USERNAME and PASSWORD!")
if options.quiet == True and not options.outfile:
print("WARNING: Quiet mode enabled with no output file specified. There will be no logging.")
return options
if __name__ == '__main__':
opts = get_opts()
if opts.serverlist:
server = get_servers(opts.serverlist)
else:
server = opts.server
if opts.cmdlist:
command = get_cmds(opts.cmdlist)
else:
command = opts.command
for s in server:
print("Running on "+s)
tn = telnetlib.Telnet(s)
tel_login()
for c in command:
print("Running "+c+" on "+s)
data = send_cmd(c)
if not opts.quiet:
print data
if opts.outfile:
do_log(data)
tn.close()