Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Using paramiko module of Python and threading to realize multi-thread login to multiple Linux servers

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Sometimes we need to execute the same command or the same operation on multiple Linux servers, which is too troublesome if we log on to each one separately, so we can consider using automated scripts to do so. I use Python multithreading here, which is faster. If you can only use Shell, it is inefficient to execute the other machine.

In response to this need, I wrote a Python script to accomplish this kind of work, which roughly achieved the effect I needed.

Implementation code:

#! / usr/bin/python#*-*coding:utf8*-* "" this script is suitable for bulk login to the Linux operating system, and executing some simple commands requires that all servers have the same user name and password, or the same key, and that the command executed by logging in the same way can only enter the first line by default. If you want to output multiple lines, you need to modify the ssh_login function. The default script is to log in with a user name and password. You can also use the key login option to specify:-c: followed by a command to execute, such as wrecoveryuptime.hostname, date, etc. If there is a space in the command, you need to use double quotes, such as "cat / etc/hosts"-a: followed by a host, there are three words: 192.168.1.31 refers to a single host, 192.168.1.31192.168.132, specify multiple hosts, replace the comma with "-" if there are many hosts in the specified interval However, the user name and password are different, and the login method is also different. You can consider writing this information to a file. Through the need for file traversal to complete a complex environment, import paramikofrom optparse import OptionParserimport sysimport netaddrimport threading# use option help information can be used in Chinese reload (sys) sys.setdefaultencoding ("utf-8") # to define the option usage = sys.argv [0] + "[option]" parser = OptionParser (usage) parser.add_option ('- centering,'--commond', dest='commond', action='store' Default=False, help=', the order you want to carry out For example, uptime') parser.add_option ('- averse,'--host', dest='host', action='store', default=False, help=') requires the host to execute the command, and the IP address of the host, with multiple IP separated by commas You can also use "-" to connect to a host scope') options, args = parser.parse_args () def ssh_login (ip, commond): "Log in and execute the command Can be changed to log in using the key "ssh = paramiko.SSHClient () ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ()) ssh.connect (hostname=ip, port=22, username='root', password='p-0p-0p-0') stdin, stdout, stderr = ssh.exec_command (commond) print ('% s\ t% s'% (ip)" Stdout.readline () .rstrip () ssh.close () if _ _ name__ ='_ _ main__': if not options.commond: print ("Please specify the command to be executed") exit (1) if not options.host: print ("Please specify host") exit (1) if','in options.host: ip = options.host.split (' ') for i in ip: t = threading.Thread (target=ssh_login, args= (I, options.commond)) # execute using threads Faster t.start () elif'-'in options.host: startip = options.host.split ('-') [0] endip = options.host.split ('-') [1] ip = list (netaddr.IPRange (startip, endip)) # netaddr.IPRange () is used to calculate all IP for i in ip in the IP address range: t = threading.Thread (target=ssh_login Args= (str (I), options.commond)) t.start () else: ip = options.host ssh_login (ip, options.commond)

The implementation mode and results are as follows:

.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report