1.1 Introduction:
Paramiko is a module written in the Python language that follows the SSH2 protocol and supports the connection of remote servers in a way that is encrypted and authenticated.
All Python-supported platforms, such as Linux, Solaris, BSD, MacOS X, Windows, and so on, are supported by the use of Python, which can run across platforms, so Paramiko Paramiko is one of the best tools when you need to use SSH to connect to another platform from one platform and perform a series of operations.
1.2 Installation:
There are two prerequisites for installing Paramiko, Python and another module named Pycrypto.
The standard Python module is usually installed and only needs to be run in the root directory of the module:
Python setup.py Build
Python setup.py Install
Note: Check the installation of GCC (yum-y install gcc) before installing
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
(Compile times Error:error:command ' gcc ' failed with exit status 1; This is due to a missing Python-dev 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 '
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 isn't None and not _fastmath. Have_decl_mpz_powm_sec:
Commented on
#if _fastmath is not None and not _fastmath. Have_decl_mpz_powm_sec:
3. Use 3.1 to execute remote command
import Paramiko
ssh = Paramiko. Sshclient ()
ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
ssh.connect ("One 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", 22))
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", 22))
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 ()
This article is from the "Heart of the Past" blog, please be sure to keep this source http://jwlin.blog.51cto.com/7560932/1787882
Learning Python Paramiko-->one