How to install the Python SSH module in Windows

Source: Internet
Author: User
This article describes how to install the Python SSH module in a Windows system. This article covers the installation of pycrypto, ecdsa, paramiko, OpenSSH, SSH, and other modules, for more information about how to use SSH in Python, you need OpenSSH. OpenSSH depends on the paramiko module, while the paramiko module depends on the pycrypto module. Therefore, you need to use SSH in Python, install the module in the following sequence: pycrypto-> ecdsa-> paramiko

1. install pyCrypto

Installing this is troublesome and requires local Compilation. to install vs or gcc, there are a bunch of configurations, which may not necessarily be compiled successfully. (Installation steps can be found online)
We recommend that you download the compiled version: http://www.voidspace.org.uk/python/modules.shtml#pycrypto directly
Download and install it directly.
(Note: crypto has java and C ++ versions)

2. install ecdsa

I read this library not mentioned in many blogs, but when I executed paramiko, I was prompted that the ecdsa module could not be found.
Download: https://pypi.python.org/pypi/ecdsa/0.9, unzip to a directory with a setup. py.
The installation is relatively simple. in windows, run python setup. py install directly in the decompressed directory.

3. install paramiko

And install ecdsa type, but it is very slow to open the download page...

Download: https://github.com/paramiko/paramiko,

The installation procedure is the same as that of ecdsa.

Note: 1. all third-party libraries installed separately will be saved to % PYTHON_HOME % \ Lib \ site-packages by default if you do not specify the library directory after installation.

2. python is case sensitive and the module name is the same.

3. Crypto provides common encryption and decryption algorithms, such as RSA, RC4, DSA, and DES.

Test code:

The code is as follows:


#! /Usr/bin/python
#-*-Coding: UTF-8 -*-
# Cp@chenpeng.info

Import paramiko

Def MAIN ():

Host = "10.1.1.1 ″
Port = 22
User = "root"
Pswd = 111222333 ″

Ssh = paramiko. SSHClient ()
Ssh. set_missing_host_key_policy (paramiko. AutoAddPolicy ())
Ssh. connect (host, port, user, pswd)
Stdin, stdout, stderr = ssh.exe c_command ('ifconfig ')
Print stdout. read ()
Ssh. close ()
#

If _ name __= = '_ main __':
Try:
MAIN ()
Except t Exception, e:
Print e



The output is as follows:

Currently, it is mainly used to execute ssh commands on multiple servers in batches, which is convenient for batch maintenance.

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 ")


Example:

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 ()


Download files from a 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. get (remotepath, localpath)
T. close ()


Upload files to a 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.