Installation and use of Python Paramiko module

  • 2020-05-17 05:42:47
  • OfStack

1. Introduction

Common solutions require the necessary configuration of the remote server, which is not very convenient if there are only one or two remote servers, if there are N machines, which need to be configured one by one, or if you need to use the code to do the above operations. Compared with the previous method, paramiko only needs to install the corresponding software (python and PyCrypto) on the local site. There is no configuration requirement for the remote server, and it is especially helpful for connecting multiple servers and performing complex connection operations. The following article will introduce the installation and use of Python Paramiko module in detail. ,

2. Install

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

Usually the standard python module is installed and only needs to run in the root directory of the module:


python setup.py build
python setup.py install

Note: check whether gcc (yum-y install gcc) is installed before installation.

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

Testing:


python>> import Crypto

) error: command 'gcc' failed with exit status 1; This is due to the lack of python-dev software package, 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'

Testing:


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

the


if _fastmath is not None and not _fastmath.HAVE_DECL_MPZ_POWM_SEC:

The comments


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

Use 3.

3.1 execute remote commands


#!/usr/bin/python
import paramiko
 
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(" some IP address ",22," The user name ", " password ")
stdin, stdout, stderr = ssh.exec_command(" Your command ")
print stdout.readlines()
ssh.close()

3.2 upload files to remote location


#!/usr/bin/python
import paramiko
 
t = paramiko.Transport((" some IP address ",22))
t.connect(username = " The user name ", password = " 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 a remote location


#!/usr/bin/python
import paramiko
 
t = paramiko.Transport((" some IP address ",22))
t.connect(username = " The user name ", password = " password ")
sftp = paramiko.SFTPClient.from_transport(t)
remotepath='/tmp/test.txt'
localpath='/tmp/test.txt'
sftp.get(remotepath, localpath)
t.close()

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: