Detailed python execution via Paramiko module in bulk SSH commands

Source: Internet
Author: User
Multi-device Batch SSH command, is currently serial, later will join multithreading to achieve parallel, directly on the source code

Note You cannot perform dynamic commands such as top

#!/usr/bin/env python#-*-coding:utf-8-*-__author__ = ' babyshen ' __version__ = ' 1.0.0 ' Import Paramiko class SSh (object ): Def __init__ (self,port,username,password): Self.port = Port Self.username = Username SELF.PASSW Ord = password def ssh_con (self,host,ip,cmd): SSH = Paramiko. Sshclient () Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) Ssh.connect (Ip,self.port,self.username,self.password) stdin, stdout, stderr = Ssh.exec_comm and (cmd) Res,err = Stdout.read (), stderr.read () result = Res If res else err print (' \033[31;1m%s%s\03 3[0m '% (HOST,IP)) print (' \033[32;1m%s\033[0m '%result.decode ()) Ssh.close () if __name__ = = ' __main__ ': cl                    ient = {' host1 ': ' 1.1.1.1 ', ' host2 ': ' 2.2.2.2 ', ' host3 ': ' 3.3.3.3 ', ' Host4 ': ' 4.4.4.4 ',} # Host list, hostname:ip port = 22 # port number Usernam    e = ' root '        # User name password = ' 123456789 ' # password cmd = "IP route show Match 1.1.1.1 | head-1 "ssh = SSH (port,username,password) for i in Client:host,ip = I,client[i] Try:ssh        . Ssh_con (Host,ip,cmd) except Windowserror:print (' \033[31;1m%s%s%s \033[0m\n '% (host,ip, ' connection attempt failed ')) Except Exception as E:print (e)
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.