A: 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.
As a common example, there is a need to use a Windows client to connect remotely to a Linux server and view the log status above, as you would normally use:
1: Use Telnet
2: With Putty
3: With WINSCP
4: With Xmanager etc...
Now, if the demand adds another one, what do I do to download files from the server? The usual approach might be:
Install FTP on 1:linux and configure
Install Sambe on 2:linux and configure ...
You will find that the common solution will need to the remote server necessary configuration, if the remote server only one or two is good to say, if there are n units, also need to be configured by the station, or need to use code to do above, the above method is not very convenient.
The use of Paramiko can solve the above problem well, compared to the previous method, it only need to install the appropriate software (Python and Pycrypto) on-premises, there is no configuration requirements for the remote server, for connecting multiple servers, complex connection operation is particularly helpful.
Two: 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 buildpython setup.py Install
The above two commands, Paramiko and Pycrypto is no exception, the only trouble is to install Pycrypto, need to compile GCC library, if there is no GCC library error, will cause pycrypto and Paramiko can not be installed.
The following is an example of 32-bit Windows XP that illustrates the Paramiko installation process
1: Install python,2.2 above version can be, I use 2.5, installation process slightly, and assume the installation directory is C:\python.
2: Determine if GCC is installed locally, and in the path variable can be found, if not, you can use the Windows version of GCC, namely mingw,:http://sourceforge.net/projects/mingw/, Then run the downloaded EXE file for network installation, assuming the directory is C:\MINGW, add C:\mingw\bin in Path, and create a new file under C:\python\lib\distutils name is Distutils.cfg, fill in:
[Build] Compiler=mingw32
3: Download Pycrypto, address is
https://www.dlitz.net/software/pycrypto/
Install Pycrypto:
C:\python\python.exe setup.py buildc:\python\python.exe setup.py Install
Run Python.exe, and at the prompt enter:
Import Crypto
If no error message appears, the crypto installation is successful
4: Download Paramiko, address is http://www.lag.net/paramiko/
C:\python\python.exe setup.py buildc:\python\python.exe setup.py Install
Run Python.exe, and at the prompt enter:
Import Paramiko
If no error message appears, the Paramiko installation is successful
Three: Using Paramiko
If you feel that the installation of Paramiko is still a bit of trouble, when the use of the convenience provided by Paramiko will feel that this is very worthwhile.
Here are two codes for connecting to a Linux server using Paramiko
Way One:
SSH = Paramiko. Sshclient () Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) Ssh.connect (, 22,,)
The second line of code above is to allow connections to hosts that are not in the Know_hosts file.
Way two:
t = Paramiko. Transport (("Host", "Port") t.connect (username = "username", password = "password")
If a key is required to connect to the remote host, the second line of code above can be changed to:
T.connect (username = "username", password = "password", hostkey= "key")
Here's a practical example:
3.1 Windows runs arbitrary commands on Linux and outputs the results
If the Linux server has 22 ports open, on the Windows side, we can use Paramiko to connect to the server remotely, execute arbitrary commands, and then print or otherwise get the result.
The code is as follows:
Paramiko ssh = Paramiko. Sshclient () Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) Ssh.connect (, A,,) stdin, stdout, stderr = Ssh.exec_command () stdout.readlines () Ssh.close ()
One of the "your commands" can be any Linux-supported command, such as some common commands:
DF: View disk Usage uptime: Display system run time information Cat: Display a file content Mv/cp///sbin/service/xxxservice/stop/restart: Start, stop, restart a service Netstat-ntl | grep 8080: View 8080 port usage or nc-zv localhost: see usage of all ports/-name XXX: Find a file
...
As a result, almost any operation on Linux can be done through the Windows side, and if this feature is extended, you can manage multiple servers at the same time.
3.2 Downloading files from the Linux server on the widnows side
Paramiko t = Paramiko. Transport (("Host", "Port")) T.connect (username = "username", password = "password") sftp = Paramiko. Sftpclient.from_transport (t) remotepath= '/var/log/system.log ' localpath= '/tmp/system.log ' Sftp.get (RemotePath, LocalPath) T.close ()
3.3 Uploading files to a Linux server from the widnows side
Paramikot = Paramiko. Transport (("Host", "Port")) T.connect (username = "username", password = "password") sftp = Paramiko. Sftpclient.from_transport (t) remotepath= '/var/log/system.log ' localpath= '/tmp/system.log ' Sftp.put (LocalPath, RemotePath) T.close ()
My use of the Paramiko has just begun, if there is a wrong place, please correct me! Thank you, first!
Installation and use of Python-paramiko