Use Python for simple batch management of multiple hosts

Source: Internet
Author: User

1, current operating system environment

2, install the module used by Python, install using the PIP command
Yum-y Install GCC
#安装pycrypto

wget http://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.6.tar.gz

TAR-XF pycrypto-2.6.tar.gz

CD pycrypto-2.6/

Python setup.py build && python setup.py install

#测试, note case

Python>> Import Crypto

#安装 Paramiko

wget https://pypi.python.org/packages/2b/27/b64860e7b208ff1dd36fe208d07bca1f9637a11fe733e2f2ceea587c3f75/ Paramiko-1.7.5.zip

Unzip Paramiko-1.7.5.zip

CD paramiko-1.7.5

Python setup.py build && python setup.py install

#测试:

Python>> Import Paramiko

3, below we can use the Paramiko module to achieve a number of host simple batch management, I use the Python multi-threaded threading module, below is a simple demo for everyone to reference

Import Paramiko
Import threading
Import time

#首先我们先定义一个主机列表 (guaranteed to be on a server that controls other hosts can be free of key login on multiple control host, there are two ways, one is based on the user name and password, one is based on the public key authentication method)
Hosts =[(Ip1,username1,password), (Ip2,username2,password2), (IP3,USERNAME3,PASSWORD3)]

#创建ssh对象
SSH = Paramiko. Sshclient ()

Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())

def run (host_info,cmd):
Ip,username,password = Hostinfo
Ssh.connect (Ip,username,pasword)
Stdin,stdout,stderro = Ssh.exec_command (cmd)
Cmd_result = Stdout.read (), Stderr.read ()
Print Cmd_result
Return Cmd_result

For I in hosts:
t = Threading. Thread (target=run,args=[i, ' you input cmd ')
T.start ()

4, through the above example basically and realize a multi-host simple batch management, of course you can extend the more specific complex

Use Python for simple batch management of multiple hosts

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.