Home >Backend Development >Python Tutorial >Detailed explanation of the installation and use of Python Paramiko module

Detailed explanation of the installation and use of Python Paramiko module

WBOY
WBOYOriginal
2016-12-05 13:27:221787browse

1. Foreword

Common solutions require necessary configuration of the remote server. If there are only one or two remote servers, it is okay. If there are N servers, you need to configure them one by one, or if you need to use code to perform the above operations, the above method will not work. So convenient. Using paramiko can solve the above problems very well. Compared with the previous method, it only needs to install the corresponding software (python and PyCrypto) locally, and has no configuration requirements for remote servers. For connecting multiple servers, complex connections are required. Operations are particularly helpful. The following article will introduce the installation and use of the Python Paramiko module in detail, let’s learn together. ,

2. Installation

There are two prerequisites for installing paramiko, python and another module called PyCrypto.

Normally to install standard python modules, just run in the module's root directory:

python setup.py build
python setup.py install

Note: Check whether 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:

python>> import Crypto

(Error when compiling: error: command 'gcc' failed with exit status 1; This is because the python-dev package is missing, so 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 attribute '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 it

if _fastmath is not None and not _fastmath.HAVE_DECL_MPZ_POWM_SEC:

Annotated

#if _fastmath is not None and not _fastmath.HAVE_DECL_MPZ_POWM_SEC:

3. Use

3.1 Execute remote commands

#!/usr/bin/python
import paramiko
 
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("某IP地址",22,"用户名", "口令")
stdin, stdout, stderr = ssh.exec_command("你的命令")
print stdout.readlines()
ssh.close()

3.2 Upload files to remote

#!/usr/bin/python
import paramiko
 
t = paramiko.Transport(("某IP地址",22))
t.connect(username = "用户名", password = "口令")
sftp = paramiko.SFTPClient.from_transport(t)
remotepath='/tmp/test.txt'
localpath='/tmp/test.txt'
sftp.put(localpath,remotepath)
t.close()

3.3 Download files from remote

#!/usr/bin/python
import paramiko
 
t = paramiko.Transport(("某IP地址",22))
t.connect(username = "用户名", password = "口令")
sftp = paramiko.SFTPClient.from_transport(t)
remotepath='/tmp/test.txt'
localpath='/tmp/test.txt'
sftp.get(remotepath, localpath)
t.close()

Summary

The above is the entire content of this article. I hope the content of this article can bring some help to everyone's study or work. If you have any questions, you can leave a message to communicate.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn