First, the preface
Common solutions will require the necessary configuration of the remote server, if the remote server only one or two good said, if there are N, but also need to configure each platform, or need to use code to do the above, the above approach is not very convenient. The use of Paramiko can solve the above problem, compared to the previous method, it only needs to install the appropriate software (Python and Pycrypto), the remote server does not have the configuration requirements, for connecting multiple servers, the complex connection operation is particularly helpful. The following article on the detailed introduction of Python Paramiko module installation and use, learn together. 、
Second, the installation
There are two prerequisites for installing Paramiko, Python and another module called Pycrypto.
Standard Python modules are typically installed and run only in the root directory of the module:
Python setup.py build
python setup.py install
Note: check to see if GCC is installed before installation (yum-y install GCC)
2.1 Pycrypto Installation
wget http://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.6.tar.gz
tar-zxvf pycrypto-2.6.tar.gz
CD pycrypto-2.6/
python setup.py build && python setup.py install
Test:
(Compile times Error: Error:command ' gcc ' failed with exit status 1; This is due to the lack of Python-dev packages, yum-y install python-devel)
2.2 Paramiko Installation
wget http://www.lag.net/paramiko/download/paramiko-1.7.7.1.tar.gz
tar xvzf paramiko-1.7.7.1.tar.gz
CD PARAMIKO-1.7.7.1/
python setup.py build && python setup.py install
Crypto error: ' Module ' object has no at Tribute ' Have_decl_mpz_powm_sec '
Test:
Python>> Import Paramiko
(Crypto error: ' Module ' object has no attribute ' have_decl_mpz_powm_sec '
Find/usr/lib/python2.7/site-packages/crypto/util/number.py
Put
If _fastmath is not-None and not _fastmath. Have_decl_mpz_powm_sec:
Commented on
#if _fastmath is not None and not _fastmath. Have_decl_mpz_powm_sec:
Third, the use
3.1 Execute remote command
#!/usr/bin/python
import Paramiko
ssh = Paramiko. Sshclient ()
ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
ssh.connect ("An IP address", 22, "username", "password")
stdin, stdout, stderr = Ssh.exec_command ("Your Command")
print stdout.readlines ()
ssh.close ()
3.2 Uploading files to remote
#!/usr/bin/python
Import Paramiko
t = Paramiko. Transport ("An IP Address,")
t.connect (username = "username", password = "password")
sftp = Paramiko. Sftpclient.from_transport (t)
remotepath= '/tmp/test.txt '
localpath= '/tmp/test.txt ' Sftp.put
( Localpath,remotepath)
T.close ()
3.3 Downloading files from remote
#!/usr/bin/python
Import Paramiko
t = Paramiko. Transport ("An IP Address,")
t.connect (username = "username", password = "password")
sftp = Paramiko. Sftpclient.from_transport (t)
remotepath= '/tmp/test.txt '
localpath= '/tmp/test.txt ' Sftp.get
( RemotePath, LocalPath)
t.close ()
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.