-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_tn.py
106 lines (86 loc) · 2.29 KB
/
auto_tn.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
#!/usr/bin/env python
import os
import sys
import telnetlib
import multiprocessing as mp
def tnOne(host):
oldStdout = sys.stdout
sys.stdout = open(host,"w")
t = telnetlib.Telnet()
t.open(host)
t.write("\n")
response = t.read_until(login_prompt, 10)
if login_prompt in response:
print response + str(username)
else:
return 0
t.write("%s\n" % username)
response = t.read_until(password_prompt, 10)
if password_prompt in response:
print password_prompt + str(passwd)
else:
return 0
t.write("%s\n" % passwd)
response = t.read_until(command_prompt, 600)
if command_prompt not in response:
return 0
else:
print command_prompt
for cmd in cmd_list:
t.write("%s\n" % cmd)
response = t.read_until(command_prompt, 600)
if command_prompt not in response:
return 0
print response
t.close()
sys.stdout = oldStdout
return 1
basename = os.path.splitext(os.path.basename(sys.argv[0]))[0]
import getopt
optlist,left = getopt.getopt(sys.argv[1:], 'h:l:f:c:u:p:')
usage = """
usage: %s [-h host] [-l hostfile] [-f cmdfile] [-c "command"] [-u username] [-p passowrd]
-c command
-f command file
-h single hostname
-l hostname file
-u username
-p password
Example: %s -c "echo $HOME" -u %s -p password
""" % (basename, basename, "root")
if len(sys.argv) < 2:
print usage
sys.exit(1)
host_list = [ ]
cmd_list = [ ]
for opt, optarg in optlist:
if opt == '-f':
for r in open(optarg):
r = r.strip()
if r:
cmd_list.append(r)
elif opt == '-c':
command = optarg
if command[0] == '"' and command[-1] == '"':
command = command[1:-1]
cmd_list.append(command)
elif opt == '-h':
host_list = [optarg]
elif opt == '-l':
for h in open(optarg):
h = h.strip()
if h:
host_list.append(h)
elif opt == '-u':
username = optarg
elif opt == '-p':
passwd = optarg
timeout = 600
login_prompt = "login:"
password_prompt = "Password:"
command_prompt = "#"
print (cmd_list)
bulk_tn = mp.Pool(processes=100)
bulk_tn.map_async(tnOne, host_list)
bulk_tn.close()
bulk_tn.join()