Python Paramiko multi-threaded bulk Modify Host Account password

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.