The paramiko module installs and USES the of remote login server

  • 2020-04-02 13:25:25
  • OfStack

A:

Paramiko is a module written in python that follows the SSH2 protocol and supports connection to remote servers in the form of encryption and authentication.

Since we use a cross-platform language like python, paramiko can support all platforms supported by python, such as Linux, Solaris, BSD, MacOS X, Windows, etc. Therefore, paramiko is one of the best tools if we need to use SSH to connect from one platform to another for a series of operations.

A common example of this would be a requirement to use a Windows client, remotely connect to a Linux server, and check the log status.

1: use Telnet

2: use the PUTTY

3: use WinSCP

4: use XManager...

Now what if you add a new requirement to download a file from the server? The usual solution might be:

1: install and configure FTP on Linux

2: install Sambe on Linux and configure...

You will find that common solutions require the necessary configuration of the remote server, which is not convenient if there are only one or two remote servers, if there are N remote servers, you need to configure one by one, or you need to use the code to do the above.

Paramiko is a good solution to this problem, requiring only the local installation of the appropriate software (python and PyCrypto) compared to the previous approach. It has no configuration requirements for remote servers, and is especially helpful for complex connection operations to connect multiple servers.

2: install

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

Standard python modules are usually installed and run in the root of the module:


python setup.py build
python setup.py install

The above two commands are ok, paramiko and PyCrypto are no exception, the only trouble is to install PyCrypto, need to compile the GCC library, if there is no GCC library will report an error, will cause PyCrypto and paramiko can not be installed.

The following is a 32-bit Windows XP example to illustrate the paramiko installation process

1: install python, version 2.2 or above, I used 2.5, the installation process is omitted, and assume the installation directory is c: python.

2: determine whether GCC is installed locally, and the PATH variable can be found, if not, you can use the Windows version of GCC, namely MinGW, download address: http://sourceforge.net/projects/mingw/, then run the exe file after download network installation, assuming that the directory to C: \ mingw, join in the PATH C: \ mingw \ bin, and in the C: \ python \ lib \ distutils under a new name is distutils. The CFG file, fill in:


[build] 
compiler=mingw32
 

3: download PyCrypto, address is

https://www.dlitz.net/software/pycrypto/

Install PyCrypto:

unzip
Under DOS into the unzipped directory, run


C:pythonpython.exe setup.py build
C:pythonpython.exe setup.py install
 

Install the test
Run python.exe, and at the prompt enter:


Import  Crypto

If there is no error, the Crypto installation is successful

4: download paramiko, address is http://www.lag.net/paramiko/

unzip
Under DOS into the unzipped directory, run


C:pythonpython.exe setup.py build
C:pythonpython.exe setup.py install

Test paramiko
Run python.exe, and at the prompt enter:

Import  paramiko

If there is no error, the paramiko installation is successful

 

Three: use paramiko

If installing paramiko is a bit of a hassle, it's worth it when you use the convenience that paramiko provides.

Here are two types of code that use paramiko to connect to a Linux server

A:


ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(" some IP address ",22," The user name ", " password ")

The second line of code above allows connections to hosts that are not in the know_hosts file.

Method 2:


t = paramiko.Transport(( "The host" , "Port" ))
t.connect(username =  "User name" , password =  "Password" )

If the key is needed to connect to the remote host, the second line of code above can be changed to:

t.connect(username =  "User name" , 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 port 22 open, on the Windows side, we can use paramiko to connect to the server remotely, execute any command, and get the result by print or otherwise,


#!/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()

One of the "your command" can be arbitrary Linux support commands, such as some commonly used commands:


Df: view disk usage
Uptime: displays the system running time information
Cat: displays the contents of a file
Mv /cp/mkdir/rmdir: operates on files or directories
/sbin/service/ xxxservice start/stop/restart: to start, stop, or restart a service
Netstat - NTL |grep 8080: view port 8080 usage
  Or nc-zv localhost: see usage of all ports
Find / -name XXX: find a file

As a result, almost any operation on Linux can be done on the Windows side and, by extension, multiple servers can be managed simultaneously.

3.2 download the file on the Linux server from the widnows side


#!/usr/bin/python 
import paramiko

t = paramiko.Transport(( "The host" , "Port" ))
t.connect(username =  "User name" , 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 upload files from the widnows side to the Linux server


#!/usr/bin/python 
import paramiko
t = paramiko.Transport(( "The host" , "Port" ))
t.connect(username =  "User name" , password =  "Password" )
sftp = paramiko.SFTPClient.from_transport(t)
remotepath='/var/log/system.log'
localpath='/tmp/system.log'
sftp.put(localpath,remotepath)
t.close()


Related articles: