python paramiko多線程批量修改主機賬號密碼

#!/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))


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章