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