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