#!/usr/bin/env python
#-*- coding:utf:8 -*-
import paramiko,time,threading
def reset_passwd(line):
try:
test=paramiko.Transport((line.split()[0],22))
test.connect(username=‘username‘,password=‘password‘)
chan=test.open_session()
chan.settimeout(5)
chan.get_pty()
chan.invoke_shell()
chan.send(‘passwd\n‘)
time.sleep(1)
chan.send(‘password\n‘)
time.sleep(1)
chan.send(‘newpassword\n‘)
time.sleep(1)
chan.send(‘newpassword\n‘)
time.sleep(1)
chan.close()
lock.acquire()
successful_ip_list.append(‘%s: 连接成功‘%(line.split()[0]))
lock.release()
except Exception, e:
lock.acquire()
failure_ip_list.append(‘%s: 连接失败‘%(line.split()[0]))
lock.release()
successful_ip_list = []
failure_ip_list = []
if __name__ == ‘__main__‘:
lock = threading.Lock()
threads = []
start = time.time()
# info.txt文件只有IP地址,每行一个IP地址
info = file(‘info.txt‘,‘r+‘)
for line in info.readlines():
t = threading.Thread(target=reset_passwd,args=(line,))
threads.append(t)
t.start()
for t in threads:t.join()
if not successful_ip_list:print ‘没有成功连接信息‘
else:
for line in successful_ip_list:print line
print ‘-‘*20
if not failure_ip_list:print ‘没有失败连接信息‘
else:
for line in failure_ip_list:print line
successful_number = len(successful_ip_list)
failure_number = len(failure_ip_list)
end = time.time()
print "成功连接 %s 台服务器,失败连接 %s 台服务器。共用时 %s 秒"%(successful_number,failure_number,(end - start))