Python remote bulk multithreading Paramiko and threading cases

Source: Internet
Author: User
Tags gluster


Preliminary understanding of the benefits of multithreading







These two examples tell us the same thing, one uses 8s a 5s which is the benefit of multithreading concurrency execution.



Basic cases of Paramiko and threading multithreaded Remote execution
--[[email protected] pythontest]# cat paramiko-threading.py
#!/usr/bin/python
#coding: Utf-8
#from settings.py Import *
Import Paramiko
Import threading
Import time
def tunction (Ip,username,password,command):
Client = Paramiko. Sshclient ()
Client.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
Client.connect (ip,username= Username,password=password)
Stdin,stdout,stdeer = Client.exec_command (command)
Print Stdout.read ()
Client.close ()
def main (Host_list,command):
Thread_list = []
For Ip,username,password in Host_list:
t = Threading. Thread (target = Tunction,args = (ip,username,password,command))
Thread_list.append (t)
For th in Thread_list:
Th.start ()
For th in Thread_list:
Th.join ()



If name = = "main":


host_list = [
    ("10.133.107.8","gluster_zabbix","gluster"),
    ("10.133.107.9","gluster_zabbix","gluster"),
]
command = "ls /tmp/"
main(host_list,command)


==============================
Optimization: When we have a lot of machines and we need to change them frequently we can make a profile individually
To create a settings.py configuration file
Directory structure

--[[email protected] pythontest]# cat settings.py
#!/usr/bin/python
#coding: Utf-8



Host_list = [
("10.13.17.8", "Glustqeqr_zabbix", "Gluqwester"),
("10.13.17.9", "Glustewrer_zabbix", "Glursdaster"),



]
Command = "ls/var/"



Code
--[[email protected] pythontest]# cat paramiko-threading.py
#!/usr/bin/python
#coding: Utf-8
From Settings Import *
Import Paramiko
Import threading
Import time
def tunction (Ip,username,password,command):
Client = Paramiko. Sshclient ()
Client.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
Client.connect (ip,username= Username,password=password)
Stdin,stdout,stdeer = Client.exec_command (command)
Print Stdout.read ()
Client.close ()
def main (Host_list,command):
Thread_list = []
For Ip,username,password in Host_list:
t = Threading. Thread (target = Tunction,args = (ip,username,password,command))
Thread_list.append (t)
For th in Thread_list:
Th.start ()
For th in Thread_list:
Th.join ()



If name = = "main":


main(host_list,command)


Python remote bulk multithreading Paramiko and threading cases


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.