#!/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: Connection succeeded '% (Line.split () [0]))
Lock.release ()
Except Exception, E:
Lock.acquire ()
Failure_ip_list.append ('%s: Connection failed '% (line.split () [0]))
Lock.release ()
Successful_ip_list = []
Failure_ip_list = []
if __name__ = = ' __main__ ':
Lock = Threading. Lock ()
Threads = []
Start = Time.time ()
# info.txt file only IP address, one IP address per line
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 ' No successful connection information '
Else
For line in Successful_ip_list:print line
print '-' *20
If not Failure_ip_list:print ' no failed connection information '
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 "Successfully connected to the%s server and failed to connect to the%s server." Total%s seconds "% (Successful_number,failure_number, (End-start))
This article is from the ", Mineral water" blog, please be sure to keep this source http://guwenqiang.blog.51cto.com/5462040/1709668
Python Paramiko multi-threaded bulk Modify Host Account password