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.
:
https://www.dlitz.net/software/pycrypto/
http://www.lag.net/paramiko/
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
First install the Pycrypto, then install the Paramiko
Three: Using Paramiko
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")
Instance:
SSH = Paramiko. Sshclient () Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) Ssh.connect (, A,,) stdin, stdout, stderr = Ssh.exec_command () stdout.readlines () Ssh.close ()
This article is from the "Jeff" blog, so be sure to keep this source http://zhangxz.blog.51cto.com/5490116/1567372
Python installation Paramiko module