tutorial on installing Python's SSH module under Windows system

Source: Internet
Author: User
The use of SSH in Python requires openssh, and OpenSSH relies on the Paramiko module, and the Paramiko module relies on the Pycrypto module, so to use SSH in Python, you need to first install the module order: Pycrypto- > ECDSA, Paramiko

1, installation Pycrypto

Installing this is cumbersome and requires local compilation, and there is a lot of configuration for VS or GCC, and it's not necessarily possible to compile successfully. (Can search the installation steps on the Internet)
It is recommended to download the compiled version directly: Http://www.voidspace.org.uk/python/modules.shtml#pycrypto
Download directly and install both.
(Note: Crypto has Java and C + + editions)

2, Installation ECDSA

See a lot of blogs do not mention this library, but I do Paramiko, I am not prompted to find the ECDSA module.
Download: https://pypi.python.org/pypi/ecdsa/0.9, unzip to a directory, there is a setup.py in the directory.
Installation is relatively simple, Windows directly under the extracted directory after the execution: Python setup.py install

3. Installing Paramiko

With the ECDSA type installed, just open the download page very slow ...

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

Installation Steps with ECDSA

Note: 1, all additional third-party libraries installed, if not specified after the installation of the Library directory, will be saved by default to%python_home%\lib\site-packages.

2, Python is case sensitive, the module name is also.

3, Crypto can provide common encryption and decryption algorithms, such as: RSA, RC4, DSA, 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.exec_command (' ifconfig ')
Print Stdout.read ()
Ssh.close ()
#

If __name__== ' __main__ ':
Try
MAIN ()
Except Exception,e:
Print E



The output is as follows:

Currently mainly used for batch execution of multiple server SSH commands, batch maintenance is more convenient.

Here are two codes for connecting to a Linux server using Paramiko

Way One:

The code is as follows:


SSH = Paramiko. Sshclient ()
Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
Ssh.connect ("One IP Address", 22, "username", "password")


The second line of code above allows you to connect hosts that are not in the Know_hosts file

Way two:

The code is as follows:


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:

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 ("One IP Address", 22, "username", "password")
stdin, stdout, stderr = Ssh.exec_command ("Your command") print Stdout.readlines ()
Ssh.close ()


Download files on 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 ()


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

  • 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.