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.