Install and use the paramiko module (remotely log on to the server)

Source: Internet
Author: User
Paramiko is a module written in python. it complies with the SSH2 protocol and supports remote server connection through encryption and authentication. The following describes how to use paramiko.

Paramiko is a module written in python. it complies with the SSH2 protocol and supports remote server connection through encryption and authentication.

Because python is a language that can run across platforms, all platforms supported by python, such as Linux, Solaris, BSD, MacOS X, and Windows, and paramiko are supported. therefore, paramiko is one of the best tools to connect to another platform using SSH for a series of operations.

For example, you need to use a windows client to remotely connect to a Linux server and view the preceding log status:

1: telnet

2: PUTTY

3: Use WinSCP

4: Use XManager, etc...

What should I do if I need to download another file from the server? The common method may be:

1: install and configure FTP on Linux

2: install Sambe on Linux and configure...

You will find that the common solution requires the necessary configuration for the remote server. if there are only one or two remote servers, you should configure them one by one if there are N servers, or you need to use the code for the above operations, the above method is not very convenient.

Paramiko can be used to solve the above problems. compared with the previous method, it only needs to install the corresponding software (python and PyCrypto) locally, and there is no configuration requirement for the remote server, complex connection operations are especially helpful for connecting multiple servers.

II. Installation

There are two prerequisites for installing paramiko: python and another module named PyCrypto.

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

The code is as follows:


Python setup. py build

Python setup. py install

The preceding two commands are fine. paramiko and PyCrypto are no exception. The only trouble is that the GCC Library compilation is required when PyCrypto is installed. if there is no GCC library, an error is reported, which will cause PyCrypto and paramiko to be unable to be installed.

The following uses 32-bit windows XP as an example to describe the installation process of paramiko.

1: You can install python in python 2.2 or later versions. I am using python 2.5 and the installation process is omitted. assume that the installation directory is c: \ python.

2: Check whether GCC is installed locally and find it in the PATH variable. if not, use GCC for windows, that is, MinGW,: Running C: \ mingw \ bin, in c: \ python \ lib \ distutils, create a name named distutils. fill in the cfg file:

The code is as follows:


[Build]
Compiler = mingw32

3: Download PyCrypto

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

Install PyCrypto:

Extract
Enter the decompressed directory in dos and run

The code is as follows:


C: \ python \ python.exe setup. py build
C: \ python \ python.exe setup. py install

Installation Test
Run python.exe and enter:

The code is as follows:


Import Crypto


If no error message is displayed, the Crypto installation is successful.

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

Extract
Enter the decompressed directory in dos and run

The code is as follows:


C: \ python \ python.exe setup. py build
C: \ python \ python.exe setup. py install


Test paramiko
Run python.exe and enter:

The code is as follows:


Import paramiko


If no error message is displayed, paramiko is successfully installed.

III. use paramiko

If you feel it is a little troublesome to install paramiko, it is worthwhile to use the convenience provided by paramiko.

The following are two types of codes for connecting to the linux server using paramiko:

Method 1:

The code is as follows:


Ssh = paramiko. SSHClient ()
Ssh. set_missing_host_key_policy (paramiko. AutoAddPolicy ())
Ssh. connect ("an IP address", 22, "user name", "password ")


The second line of code above is used to allow connection to hosts that are not in the know_hosts file.

Method 2:

The code is as follows:


T = paramiko. Transport ("host", "Port "))
T. connect (username = "username", password = "password ")


If you need to provide a key to connect to the remote host, the second line of code above can be changed:

The code is as follows:


T. connect (username = "username", password = "password", hostkey = "key ")


The following is an example:

3.1 run any command on linux in windows and output the result

If Port 22 is opened on the linux server, we can remotely connect to the server using paramiko on the windows Server and execute any command to obtain the result through print or other methods,

The code is as follows:


#! /Usr/bin/python
Import paramiko

Ssh = paramiko. SSHClient ()
Ssh. set_missing_host_key_policy (paramiko. AutoAddPolicy ())
Ssh. connect ("an IP address", 22, "user name", "password ")
Stdin, stdout, stderr = ssh.exe c_command ("Your Command ")
Print stdout. readlines ()
Ssh. close ()

The "Your Command" can be any commands supported by linux, such as some common commands:


Df: View disk usage
Uptime: displays the system running time.
Cat: displays the content of a file.
Mv/cp/mkdir/rmdir: Operation on files or directories
/Sbin/service/xxxservice start/stop/restart: start, stop, and restart a service.
Netstat-ntl | grep 8080: View port 8080 usage
Or nc-zv localhost: view the usage of all ports
Find/-name XXX: find a file

In this way, almost all linux operations can be completed through windows. if you extend this function, you can also manage multiple servers at the same time.

3.2 Download files on linux server from widnows

The code is as follows:


#! /Usr/bin/python
Import 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 Upload files from the widnows end to the linux server

The code is as follows:


#! /Usr/bin/python
Import 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. put (localpath, remotepath)
T. close ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.