-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModPass.py
executable file
·326 lines (278 loc) · 9.94 KB
/
ModPass.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
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
# -*- set coding:utf-8 -*-
'''
Created on 2014��5��20��
@author: Administrator
'''
import PyLogin
import sys
import time
import os
import getpass
import base64
import re
import binascii
import shelve
import MyLogger
import pxssh
import threading
import MyLogger
try:
import pexpect
except ImportError:
print 'no module named pexpect'
try:
import MySQLdb
except ImportError:
print 'no module named MySQLdb'
try:
import termios,fcntl,struct,signal
except ImportError:
print 'no module named termios,fcntl,struct,signal'
def help(rtn):
if rtn == 0:
print '''
Usage:
modify servers' password
'''
elif rtn == 1:
print '''
wrong IP
'''
elif rtn == 2:
print '''
Config file Syntax error
'''
elif rtn == 3:
print '''
Please in put yourname and password
'''
elif rtn == 4:
print '''
Please check user privilege
'''
elif rtn == 5:
print '''
t_UserAuth duplicate data
'''
elif rtn == 6:
print '''
No servers or t_HostPasswd duplicate data
'''
elif rtn == 7:
print '''
username or passwd invalid
'''
elif rtn == 8:
print '''
modify password failed
'''
elif rtn == 9:
print '''
Not the supper user
'''
elif rtn == 10:
print '''
t_LoginUser miss user data
'''
elif rtn == 11:
print '''
Localdir is not a directory
'''
elif rtn == 12:
print '''
Localfile is not exist or Remotedir permission denied
'''
def GetPass(ipaddr):
try:
conn = MySQLdb.connect(ConF['host'],ConF['user'],ConF['passwd'],ConF['db'],port=3306,charset='utf8')
cursor = conn.cursor()
except Exception,e:
print e
sys.exit()
cursor.execute('select * from t_HostPasswd where IP=\'%s\' ' % (ipaddr))
thost = cursor.fetchall()
if len(thost) == 1:
Comuser = thost[0][1]
Compass = thost[0][2]
Spasswd = thost[0][3]
return Comuser,Compass,Spasswd
else:
help(6)
exit()
def updateDB(PassF):
try:
conn = MySQLdb.connect(ConF['host'],ConF['user'],ConF['passwd'],ConF['db'],port=3306,charset='utf8')
cursor = conn.cursor()
except Exception,e:
print e
sys.exit()
try:
ipaddress = PassF[0]
Comuser = PassF[1]
Compass = PassF[2]
Spasswd = PassF[3]
SQL1 = 'select ip from t_HostPasswd where ip=\'%s\'' % ipaddress
cursor.execute(SQL1)
if len(cursor.fetchall()) == 1:
SQL2 = 'update t_HostPasswd set user=\'%s\',password=\'%s\',spassword=\'%s\' where ip=\'%s\'' % \
(Comuser,PyLogin.MyCrypt('encode',Compass),PyLogin.MyCrypt('encode',Spasswd),ipaddress)
cursor.execute(SQL2)
print '----DB update execute ok -----'
except EOFError:
exit()
def Fork_2(*PassF):
#登陆变慢,而且也不能知道
AllInfo = GetPass(PassF[0]) #获取主机用户信息及root权限
Comuser = AllInfo[0]
Compass = PyLogin.MyCrypt('decode', AllInfo[1])
Spasswd = PyLogin.MyCrypt('decode', AllInfo[2])
Logger = MyLogger.MyLogger('ModPass.log','ModPass')
try:
if Comuser != 'root':
ssh = pxssh.pxssh()
ssh.login(PassF[0],Comuser,Compass,original_prompt='[\$#>?]')
ssh.sendline('export LC_ALL=zh_CN.UTF-8') #设置字符集
ssh.prompt(timeout=5)
ssh.sendline('su - root')
ssh.prompt(timeout=5)
ssh.sendline(Spasswd)
ssh.prompt(timeout=3)
ssh.sendline('echo "%s"|passwd %s --stdin' % (PassF[2],PassF[1]))
ssh.sendline('echo "%s"|passwd root --stdin' % (PassF[3]))
ssh.prompt(timeout=3)
content = ssh.before
Logger.Logger(content)
updateDB(PassF)
print '--- %s update password success ---' % PassF[0]
except Exception,e:
Logger.Logger(e)
print '--- %s update password failure ---' % PassF[0]
def Fork(*PassF):
AllInfo = GetPass(PassF[0]) #获取主机用户信息及root权限
Comuser = AllInfo[0]
Compass = PyLogin.MyCrypt('decode', AllInfo[1])
Spasswd = PyLogin.MyCrypt('decode', AllInfo[2])
Logger = MyLogger.MyLogger('ModPass.log','ModPass')
if Comuser != 'root':
ssh = pexpect.spawn('ssh %s@%s' % (Comuser,PassF[0]))
try:
i = ssh.expect(['assword:', 'continue connecting (yes/no)?','\$'], timeout=5)
if i == 0:
ssh.delaybeforesend = 0.05
ssh.sendline(str(Compass))
ssh.sendline('export LC_ALL=zh_CN.UTF-8') #设置字符集
i2 = ssh.expect(['\$',pexpect.EOF],timeout=15)
if i2 == 0:
ssh.sendline('su - root')
ssh.expect(['word:',':'],timeout=15)
ssh.delaybeforesend = 0.05
ssh.sendline(str(Spasswd))
elif i == 1:
ssh.sendline('yes')
ssh.expect('assword:')
ssh.delaybeforesend = 0.05
ssh.sendline(str(Compass))
ssh.sendline('export LC_ALL=zh_CN.UTF-8')
i2 = ssh.expect(['\$',pexpect.EOF],timeout=15)
if i2 == 0:
ssh.sendline('su - root')
ssh.expect(['word:',':'],timeout=15)
ssh.delaybeforesend = 0.05
ssh.sendline(str(Spasswd))
elif i == 2:
ssh.sendline('export LC_ALL=zh_CN.UTF-8')
ssh.sendline('su - root')
ssh.expect(['word:',':'],timeout=15)
ssh.delaybeforesend = 0.05
ssh.sendline(str(Spasswd))
try:
ssh.sendline('echo "%s"|passwd %s --stdin' % (PassF[2],PassF[1]))
ssh.sendline('echo "%s"|passwd root --stdin' % (PassF[3]))
ssh.delaybeforesend = 0.5
content = ssh.before
Logger.Logger(content)
updateDB(PassF)
print '--- %s update password exec ok ---' % PassF[0]
except:
sys.exit()
except (KeyboardInterrupt,EOFError):
print '--- %s update password failure ---' % PassF[0]
exit()
except pexpect.EOF:
print "Connect Error"
print '--- %s update password failure ---' % PassF[0]
ssh.close()
except pexpect.TIMEOUT:
print 'TIMEOUT'
print '--- %s update password failure ---' % PassF[0]
ssh.close()
else:
print 'comuser == root'
ssh = pexpect.spawn('ssh %s@%s' % (Comuser,PassF[0]))
try:
i = ssh.expect(['assword:', 'continue connecting (yes/no)?','\$'], timeout=5)
if i == 0:
ssh.delaybeforesend = 0.05
ssh.sendline(str(Compass))
ssh.sendline('export LC_ALL=zh_CN.UTF-8') #设置字符集
elif i == 1:
ssh.sendline('yes')
ssh.expect('assword:')
ssh.delaybeforesend = 0.05
ssh.sendline(str(Compass))
ssh.sendline('export LC_ALL=zh_CN.UTF-8')
elif i == 2:
ssh.sendline('export LC_ALL=zh_CN.UTF-8')
try:
ssh.sendline('useradd "%s";echo "%s"|passwd "%s" --stdin ' % (PassF[1],PassF[2],PassF[1]))
ssh.sendline('echo "%s"|passwd root --stdin' % (PassF[3]))
updateDB(PassF)
print '--- %s update password exec ok ---' % PassF[0]
except:
print '--- %s update password failure ---' % PassF[0]
sys.exit()
except (KeyboardInterrupt,EOFError):
print '--- %s update password failure ---' % PassF[0]
exit()
except pexpect.EOF:
print "Connect Error"
print '--- %s update password failure ---' % PassF[0]
ssh.close()
except pexpect.TIMEOUT:
print 'TIMEOUT'
print '--- %s update password failure ---' % PassF[0]
ssh.close()
def Init():
#配置文件及时间的变量
global ConF
global fname
#配置文件
Config = '/opt/PyLogin/.config.ini'
ConF = {}
threads = []
fn = open(Config,'r')
for i in fn.readlines():
if i.find('DATABASE') == 0 :
ConF['db'] = PyLogin.MyCrypt('decode',i.strip('\n').split(':')[1])
if i.find('USER') == 0 :
ConF['user'] = PyLogin.MyCrypt('decode',i.strip('\n').split(':')[1])
if i.find('PASSWD') == 0:
ConF['passwd'] = PyLogin.MyCrypt('decode',i.strip('\n').split(':')[1])
if i.find('HOST') == 0:
ConF['host'] = PyLogin.MyCrypt('decode',i.strip('\n').split(':')[1])
try:
if ConF['db'] == '' or ConF['user'] == '' or ConF['passwd'] == '' or ConF['host'] == '' :
help(2)
exit()
except (NameError,KeyError),e:
help(2)
fn.close()
PassFile = raw_input('PassFile:')
with open(PassFile) as fobj:
for eachline in fobj:
work_thread = threading.Thread(target=Fork,args=eachline.split())
work_thread.start()
threads.append(work_thread)
time.sleep(1)
if __name__ == '__main__':
Init()