Reprinted please indicate the source: http://blog.csdn.net/jmppok/article/details/17588381
In windows, you can use putty to connect to a linux host through ssh. However, putty has some problems, such as not saving the session and garbled characters.
I accidentally found that Python also has an ssh module. I learned how to use python to write an ssh client similar to putty.
1. Prepare the environment
Python2.7 + PyCrypto + paramiko + ecdsa
The next three are python extension modules that implement the ssh connection function.
You need to download and install these three modules separately.
PyCrypto: https://www.dlitz.net/software/pycrypto/
Paramiko: http://www.lag.net/paramiko/
Edssa: https://pypi.python.org/pypi/ecdsa/0.9
Installation is very simple. Go to the module root directory and execute the python setup. py build install command.
Note: you can install the SDK on linux. In windows, I have not compiled it. You can use the compiled version to directly copy it to Lib \ site-packages.
The following is a compiled version in windows: python ssh module compiled in win7
2. Principles
1) connect to the specified host through the paramiko ssh module;
2) execute commands on the remote host through sshclient.exe c_command;
3) use the stdout, stdin, and stderr returned by exec_command to interact with each other;
4) You can run the ls command to view the host information (session) of a successful connection and the session id command to directly start a new connection;
5) It can be run in windows and linux. Pay attention to their differences when writing programs.
3. Code ssh. py
#! /Usr/bin/python #-*-coding: UTF-8-*-import OS, sysimport paramiko import threading import platformcurr_ssh = Nonecurr_prompt = ">>" # usage description def printUsage (): print "! Ls: list sessions. "print "! Session id: connect session. "print "! Conn host user password: connect host with user. "print "! Exit: exit. "# connection def conn (ip, username, passwd): try: ssh = paramiko. SSHClient () ssh. set_missing_host_key_policy (paramiko. autoAddPolicy () ssh. connect (ip, 22, username, passwd, timeout = 5) print "Connect to", ip, "", usernameglobal curr_promptcurr_prompt = username + "@" + ip + ">" return sshexcept: return None # load the previous connection information sessions = [] def loadSessions (): global sessionstry: f = open ("sessions") sessions = f. readlines () f. Close () handle T: pass # Run the local command. Command def exe_cmd_local (cmd) of ssh. py: if (cmd = "! Ls "): loadSessions () global sessionsi = 0 print" Sessions: "for s in sessions: print" [% d] % s "% (I, s) I + = 1 else: vals = cmd. split ('') if (vals [0] = "! Session "): id = (int) (vals [1]) if (id
4. Use
You can directly access the main menu via ssh. py:
Pass! Conn hZ limit? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vc3qgihvzcm5hbwugihbhc3n3zmgsvdojuw.vcd4kpha + pgltzybzcm9" http://www.bkjia.com/uploadfile/Collfiles/20131227/20131227134751392.jpg "alt =" \ ">
You can also use ssh. py host usrname passwd to directly connect to the specified client.
Yes! Ls:
Pass! Session id directly starts a new window and connects to it:
Pass! Exit.
After the connection, you can remotely execute the shell command:
5. Problems
1) Some ssh commands cannot be executed;
2) the interface display is too weak and needs to be improved.