Paramiko installation and use on Windows

Source: Internet
Author: User
Tags disk usage

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:

    • Unzip

    • Enter the extracted directory under DOS, run

C:\python\python.exe setup.py buildc:\python\python.exe setup.py Install

    • Installation test

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/

    • Unzip

    • Enter the extracted directory under DOS, run

C:\python\python.exe setup.py buildc:\python\python.exe setup.py Install
    • Test Paramiko

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:

#!/usr/bin/python
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")
For line in (Stdout.readlines ()):
Print (line,end= ")
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
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 ()



This article is from the "read" blog, please be sure to keep this source http://zcl1101017794.blog.51cto.com/7962179/1603588

Paramiko installation and use on Windows

Related Article

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.