如何用python开发一个ssh客户端工具
2018-02-26 17:39
561 查看
1)通过paramiko的ssh模块连接指定主机;
2) 通过SSHClient.exec_command在远程主机上执行命令;
3)通过exec_command返回的stdout,stdin,stderr进行交互;
4)保存成功连接的主机信息(session),可以通过ls命令查看, session id命令,直接启动新连接;
5)可在windows和linux下运行,写程序时需要注意他们的差别。
代码ssh.py
2) 通过SSHClient.exec_command在远程主机上执行命令;
3)通过exec_command返回的stdout,stdin,stderr进行交互;
4)保存成功连接的主机信息(session),可以通过ls命令查看, session id命令,直接启动新连接;
5)可在windows和linux下运行,写程序时需要注意他们的差别。
代码ssh.py
#!/usr/bin/python # -*- coding: utf-8 -*- import os,sys import paramiko import threading import platform curr_ssh = None curr_prompt = ">>" #使用说明 def printUsage(): print " !ls :list sessions." print " !session id :connect session." print " !conn host user password:connect host with user." print " !exit :exit." #连接 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," with ",username global curr_prompt curr_prompt=username+"@"+ip+">>" return ssh except: return None #加载以前的连接信息 sessions=[] def loadSessions(): global sessions try: f = open("sessions") sessions = f.readlines() f.close() except: pass #执行本地命令,ssh.py的命令 def exe_cmd_local(cmd): if(cmd == "!ls"): loadSessions() global sessions i=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<len(sessions)): os_name="platform.system()" new_console_cmd="" if(os_name="=" "linux"):="" .="" ssh.py="" "="" +="" sessions[id]+"\""="" elif(os_name="=" "windows"):="" sessions[id]="" os.system(new_console_cmd)="" else:="" print="" "didn't="" hava="" sessoin="" ",vals[1]="" elif(vals[0]="="!conn"):" global="" curr_ssh="" f="open("sessions","a")" line="vals[1]+"" "+vals[2]+"="" "+vals[3]+"\n"="" f.write(line)="" f.close()="" #在ssh连接的主机上执行命令="" def="" exe_cmd_ssh(ssh,cmd):="" if(ssh="=" none):="" connect="" to="" a="" server.="" use="" '!conn'="" please."="" return="" stdin,="" stdout,="" stderr="ssh.exec_command(cmd)" #stdin.write("y")="" #简单交互,输入="" ‘y’="" #屏幕输出="" stdout.read()="" stderr.read()="" #入口函数="" if="" __name__="='__main__':" loadsessions()="" if(len(sys.argv)="=4):" printusage()="" while="" true:="" cmd="raw_input(curr_prompt)" if(len(cmd)="=0):" continue="" if(cmd="=" "!exit"):="" if(curr_ssh="" !="None):" curr_ssh.close();="" break="" if(cmd[0]="=" '!'):="" exe_cmd_local(cmd)="" exe_cmd_ssh(curr_ssh,cmd)<="" pre="">
相关文章推荐
- 如何在windows下的Python开发工具IDLE里安装其他模块?
- 如何在windows下的Python开发工具IDLE里安装其他模块?
- 教你如何用最新的技术快速开发一个完整的Android客户端
- windows客户端开发--如何测量一个字符串显示的物理长度
- 如何在windows下的Python开发工具IDLE里安装其他模块?
- 如何在windows下的Python开发工具IDLE里安装其他模块?
- 如何在windows下的Python开发工具IDLE里安装其他模块?
- DJango-如何快速准备Python虚拟开发环境并快速定制一个项目
- 如何在windows下的Python开发工具IDLE里安装其他模块?
- smileShell 一个 ssh客户端工具
- 人生苦短,我学python之服务器如何返回一个文件内容到客户端
- Python:一个多功能的抓图工具开发(附源码)
- 将sublime 配置成python开发工具及如何更改IPython默认编辑器
- 如何 Xcode 开发工具里安装一个空的项目末模板
- Python:一个多功能的抓图工具开发(附源码)
- 如何在windows下的Python开发工具IDLE里安装其他模块?
- Java开发工具_Eclipse中如何写一个标准学生类及其测试
- 自动化运维工具ansible-如何设置客户端多python版本问题
- windows客户端开发--如何测量一个字符串显示的物理长度
- 如何在windows下的Python开发工具IDLE里安装其他模块?