Python implementation of the method of establishing an SSH connection

Source: Internet
Author: User
Tags ssh ssh server

The example in this article describes how Python implements an SSH connection. Share to everyone for your reference. The implementation methods are as follows:

I need to implement a Windows remote connection to the SSH server to execute the command function, so on the Internet to find information. My environment is: Windows7 64-bit, Python 2.7 32-bit. According to the online version, you need to download Pycrypto and Paramiko two modules for installation. The last version to download is pycrypto2.3 and paramiko1.7.6.

The installation process is also relatively simple, first install the Pycrypto after installation Paramiko, decompression at the command prompt to switch to the directory after the decompression, the input Python setup.py install can be. Install the pycrypto before installing the MinGW, or you may not be prompted for a bat file because the compiler is missing. After installing the MinGW, you need to create a distutils.cfg file in the Libdistutils folder under the Python installation directory, which reads:

[Build]

Compiler=mingw32

MinGW's download Address: http://sourceforge.net/projects/mingw/files/Automated%20MinGW%20Installer/mingw-get-inst/

Download and installation of Pycrypto and Paramiko please refer to the code for installing the Python Paramiko module under Windows

Once installed, you can write code. Since my SSH server only needs a username and password to connect, it should be said to belong to the simplest one.

Here is an example, I believe a look at the understanding:

Stdout.readlines () Returns a list, in general, the output per line of the Linux command is stored as an element and a newline character has been brought.

?

1 2 3 4 5 6 7 8 Import Paramiko client = 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 results of the output are:

?

1 2 3 4 5 6 7 >>> Total 184804-RW-------1 root 973 05-19 20:27 anaconda-ks.cfg-rw-r--r--1 root root 13895 05-19 20:27 ins tall.log-rw-r--r--1 root 3058 05-19 20:25 install.log.syslog-rw-r--r--1 root root 189008625 05-28 09:55 tmp >& Gt;>

I hope this article will help you with your Python programming.

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.