Python implements the method of establishing an SSH connection

Source: Internet
Author: User
Tags ssh server
The examples in this article describe how Python implements the SSH connection. Share to everyone for your reference. The implementation method is as follows:

I need to implement a command that is remotely connected to an SSH server under Windows, so I'm looking for information on the Internet. My environment is: Windows7 64 bit, Python 2.7 32 bit. According to the Internet, it is necessary to download Pycrypto and Paramiko two modules for installation. The last version to download is pycrypto2.3 and paramiko1.7.6.
Installation process is also relatively simple, first install pycrypto after installing Paramiko, decompression after the command prompt switch to the extracted directory, enter the Python setup.py install can be. Install the pycrypto before installing the MinGW, or you will be missing a bat file because of the lack of the compiler. After installing the MinGW, you need to create a distutils.cfg file in the Lib\distutils\ folder in the Python installation directory with the following files:
[Build]
Compiler=mingw32

MinGW's download Address: http://sourceforge.net/projects/mingw/files/Automated%20MinGW%20Installer/mingw-get-inst/
Pycrypto and Paramiko download and installation Please refer to: "Install Python Paramiko module code under Windows"

After installation, you can write code. Since my SSH server only needs a user name and password to connect, it should be said to belong to the simplest one.

Here is an example, I believe you can understand:
Stdout.readlines () returns a list in which, in general, each line of output from a Linux command is stored as an element and has a newline character.

Import paramikoclient = Paramiko. Sshclient () Client.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) client.connect (' 192.168.8.248 ', +, username= ' root ', password= ' password ', timeout=4) stdin, stdout, stderr = Client.exec_command (' ls-l ') for STD in Stdout.readlines ():  print Std,client.close ()

The result of the output is:

>>> Total 184804-rw-------1 root root    973 05-19 20:27 anaconda-ks.cfg-rw-r--r--1 root root   13895 05-19 20: install.log-rw-r--r--1 root root   3058 05-19 20:25 install.log.syslog-rw-r--r--1 root root 189008625 05-28 09:55 t Mp>>>

Hopefully this article will help you with Python programming.

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