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
#!/usr/bin/python
# coding: utf-8
import os
import sys
import subprocess
import MySQLdb
import pexpect
import struct
import fcntl
import termios
import signal
import re
import time
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
import ConfigParser
import paramiko
base_dir = "/opt/jumpserver/"
cf = ConfigParser.ConfigParser()
cf.read('%s/jumpserver.conf' % base_dir)
db_host = cf.get('db', 'host')
db_port = cf.getint('db', 'port')
db_user = cf.get('db', 'user')
db_password = cf.get('db', 'password')
db_db = cf.get('db', 'db')
log_dir = cf.get('jumpserver', 'log_dir')
user_table = cf.get('jumpserver', 'user_table')
assets_table = cf.get('jumpserver', 'assets_table')
assets_user_table = cf.get('jumpserver', 'assets_user_table')
key = cf.get('jumpserver', 'key')
class PyCrypt(object):
"""It's used to encrypt and decrypt password."""
def __init__(self, key):
self.key = key
self.mode = AES.MODE_CBC
def encrypt(self, text):
cryptor = AES.new(self.key, self.mode, b'0000000000000000')
length = 16
count = len(text)
if count < length:
add = (length - count)
text += ('\0' * add)
elif count > length:
add = (length - (count % length))
text += ('\0' * add)
ciphertext = cryptor.encrypt(text)
return b2a_hex(ciphertext)
def decrypt(self, text):
cryptor = AES.new(self.key, self.mode, b'0000000000000000')
plain_text = cryptor.decrypt(a2b_hex(text))
return plain_text.rstrip('\0')
def sigwinch_passthrough(sig, data):
"""This function use to set the window size of the terminal!"""
winsize = getwinsize()
foo.setwinsize(winsize[0], winsize[1])
def getwinsize():
"""This function use to get the size of the windows!"""
if 'TIOCGWINSZ' in dir(termios):
TIOCGWINSZ = termios.TIOCGWINSZ
else:
TIOCGWINSZ = 1074295912L # Assume
s = struct.pack('HHHH', 0, 0, 0, 0)
x = fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ, s)
return struct.unpack('HHHH', x)[0:2]
def connect_db(user, passwd, db, host='127.0.0.1', port=3306):
"""This function connect db and return db and cursor"""
db = MySQLdb.connect(host=host,
port=port,
user=user,
passwd=passwd,
db=db,
charset='utf8')
cursor = db.cursor()
return db, cursor
def run_cmd(cmd):
"""run command and return stdout"""
pipe = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if pipe.stdout:
stdout = pipe.stdout.read().strip()
pipe.wait()
return stdout
if pipe.stderr:
stderr = pipe.stderr.read()
pipe.wait()
return stderr
def connect(host, port, user, password):
"""Use pexpect module to connect other server."""
if not os.path.isdir(log_dir):
os.mkdir(log_dir)
logfile = open("%s/%s_%s_%s" % (log_dir, host, time.strftime('%Y%m%d'), user), 'a')
logfile.write('\n\n%s\n\n' % time.strftime('%Y%m%d_%H%M%S'))
cmd = 'ssh -q -p %s %s@%s' % (port, user, host)
global foo
foo = pexpect.spawn('/bin/bash', ['-c', cmd])
foo.logfile = logfile
while True:
index = foo.expect(['continue',
'assword',
pexpect.EOF,
pexpect.TIMEOUT], timeout=3)
if index == 0:
foo.sendline('yes')
continue
elif index == 1:
foo.sendline(password)
index = foo.expect(['assword',
'.*',
pexpect.EOF,
pexpect.TIMEOUT], timeout=3)
if index == 1:
signal.signal(signal.SIGWINCH, sigwinch_passthrough)
size = getwinsize()
foo.setwinsize(size[0], size[1])
foo.interact()
break
elif index == 0:
print "Password error."
break
else:
print "Login failed, please contact system administrator!"
break
foo.terminate(force=True)
def ip_all_select(username):
"""select all the server of the user can control."""
ip_all = []
ip_all_dict = {}
db, cursor = connect_db(db_user, db_password, db_db, db_host, db_port)
cursor.execute('select t2.ip, t2.comment from %s t1, %s t2, %s t3 where t1.username="%s" and t1.id=t3.uid_id and t2.id = t3.aid_id;' %
(user_table, assets_table, assets_user_table, username))
ip_all_record = cursor.fetchall()
if ip_all_record:
for record in ip_all_record:
ip_all.append(record[0])
ip_all_dict[record[0]] = record[1]
db.close()
return ip_all, ip_all_dict
def sth_select(username='', ip=''):
"""if username: return password elif ip return port"""
db, cursor = connect_db(db_user, db_password, db_db, db_host, db_port)
if username:
cursor.execute('select ldap_password from %s where username="%s"' % (user_table, username))
try:
password = cursor.fetchone()[0]
except IndexError:
password = ''
db.close()
return password
if ip:
cursor.execute('select port from %s where ip="%s"' % (assets_table, ip))
try:
port = int(cursor.fetchone()[0])
except IndexError:
port = 22
db.close()
return port
return None
def remote_exec_cmd(host, user, cmd):
jm = PyCrypt(key)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
port = sth_select(ip=host)
password = jm.decrypt(sth_select(username=username))
try:
ssh.connect(host, port, user, password)
except paramiko.AuthenticationException:
print 'Password Error .'
return None
stdin, stdout, stderr = ssh.exec_command(cmd)
print '\033[32m' + '#'*15 + ' ' + host + ' ' + '#'*15 + '\n' + '\033[0m'
output = stdout.read()
error = stderr.read()
if output:
print output
if error:
print error
print '\033[32m' + '#'*15 + ' End result ' + '#'*15 + '\n' + '\033[0m'
ssh.close()
def match_ip(all_ip, string):
ip_matched = []
pattern = re.compile(r'%s' % string)
for ip in all_ip:
if pattern.search(ip):
ip_matched.append(ip)
return ip_matched
def print_prompt():
print """
\033[1;32m### Welcome Use JumpServer To Login. ### \033[0m
1) Type \033[32mIP ADDRESS\033[0m To Login.
2) Type \033[32mP/p\033[0m To Print The Servers You Available.
3) Type \033[32mE/e\033[0m To Execute Command On Several Servers.
4) Type \033[32mQ/q\033[0m To Quit.
"""
def print_your_server(username):
ip_all, ip_all_dict = ip_all_select(username)
for ip in ip_all:
if ip_all_dict[ip]:
print "%s -- %s" % (ip, ip_all_dict[ip])
else:
print ip
def exec_cmd_servers(username):
print '\nInput the \033[32mHost IP(s)\033[0m,Separated by Commas, q/Q to Quit.\n'
while True:
hosts = raw_input('\033[1;32mip(s)>: \033[0m')
if hosts in ['q', 'Q']:
break
hosts = hosts.split(',')
hosts.append('')
hosts = list(set(hosts))
hosts.remove('')
ip_all, ip_all_dict = ip_all_select(username)
no_perm = set(hosts)-set(ip_all)
if no_perm:
print "You have no permission on %s." % list(no_perm)
continue
print '\nInput the \033[32mCommand\033[0m , The command will be Execute on servers, q/Q to quit.\n'
while True:
cmd = raw_input('\033[1;32mCmd(s): \033[0m')
if cmd in ['q', 'Q']:
break
for host in hosts:
remote_exec_cmd(host, username, cmd)
def connect_one(username, option):
ip_input = option.strip()
ip_all, ip_all_dict = ip_all_select(username)
ip_matched = match_ip(ip_all, ip_input)
ip_len = len(ip_matched)
ip = ''
if ip_len >= 1:
if ip_len == 1:
ip = ip_matched[0]
else:
if ip_input in ip_matched:
ip = ip_input
else:
for one_ip in ip_matched:
print one_ip
if ip:
password = jm.decrypt(sth_select(username=username))
port = sth_select(ip=ip)
print "Connecting %s ..." % ip
connect(ip, port, username, password)
else:
print '\033[31mNo permision .\033[0m'
if __name__ == '__main__':
username = run_cmd('whoami')
jm = PyCrypt(key)
print_prompt()
try:
while True:
option = raw_input("\033[1;32mOpt or IP>:\033[0m ")
if option in ['P', 'p']:
print_your_server(username)
continue
elif option in ['e', 'E']:
exec_cmd_servers(username)
elif option in ['q', 'Q']:
sys.exit()
else:
connect_one(username, option)
#except (BaseException, Exception):
except IndexError:
print "Exit."
sys.exit()