# -*- coding: UTF-8 -*-
import paramiko
import time ##################################################################
'''类名称: SSHCommunication
描述:SSH通讯类
作者:Sid Zhang(autopenguin)'''
##################################################################
class SSHCommunication():
def __init__(self):
self.client = paramiko.SSHClient()
##################################################################
'''方法名称: Logon
参数:HostIP->登录主机IP【字符串】
SSHPort->SSH登录端口号【数字】
Username->登录用户名【字符串】
Password->登录密码【字符串】
返回值:None
描述:SSH登录方法'''
##################################################################
def Logon(self, HostIP, SSHPort, Username, Password):
self.client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy())
self.client.connect(HostIP, SSHPort, Username, Password)
self.chan = self.client.invoke_shell()
self.chan.settimeout(120)
##################################################################
'''函数名称: Send
参数:Cmds->待发送的命令列表【字符串列表】
返回值:None
描述:发送命令方法'''
##################################################################
def Send(self, Cmds = []):
for Cmd in Cmds:
while not self.chan.send_ready():
time.sleep(0.1)
self.chan.send(Cmd + '\n')
print 'Execute Command: ' + Cmd
##################################################################
'''函数名称: SendWithoutReceive
参数:Cmds->待发送的命令列表【字符串列表】
返回值:None
描述:发送命令且无须接收结果方法'''
#################################################################
def SendWithoutReceive(self, Cmds=[]):
self.Send(Cmds)
self.Receive()
##################################################################
'''函数名称: Receive
参数:None
返回值:命令执行输出
描述:接收命令输出方法'''
##################################################################
def Receive(self):
while not self.chan.recv_ready():
time.sleep(0.1) 。
CommandOut = self.chan.recv(1024000)
CommandOutAll = CommandOut
while not (CommandOut.endswith('\x1b[m\x0f') or CommandOut.endswith('> ') or CommandOut.endswith('$') or CommandOut.endswith('# ')):
while not self.chan.recv_ready():
time.sleep(0.1)
CommandOut = self.chan.recv(102400)
CommandOutAll = CommandOutAll + CommandOut
return CommandOutAll
##################################################################
'''函数名称: Logout
参数:None
返回值:None
描述:SSH登出方法'''
##################################################################
def Logout(self):
self.chan.close()

最新文章

  1. 基于吉日嘎拉的通用权限管理Webform版老界面bug修复
  2. Android5.0新特性——图片和颜色(drawable)
  3. asp.net中分页与存储过程的一些总结
  4. input框自动填充内容背景颜色为黄色解决方法
  5. springMVC+freemarker中Could not resolve view with name... 问题解决
  6. poj3580
  7. [Redis] windows下安装 Redis
  8. Java I/O theory in system level
  9. 如何成为一名优秀的UI设计师
  10. RedissonLock分布式锁源码分析
  11. redis入门(02)redis的常见问题
  12. ABP之N层架构
  13. chrome插件学习笔记
  14. ODBC 驱动程序管理器 在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配 解决方案
  15. [IOS微信] 查看微信原始数据(含沙盒中的数据)
  16. JNI使用方法
  17. android与JS交互,互相调用方法,跳转到网页
  18. css按钮口诀 - CSS BUG顺口溜
  19. ActiveMQ 集群负载平衡
  20. POJ 2411 状压DP经典

热门文章

  1. UVa 1149 Bin Packing 【贪心】
  2. group by的使用
  3. Linux 7 中Yum 配置 说明
  4. GitHub开源库排名一百的简单介绍,值得收藏!
  5. gtid
  6. HDU 5762 Teacher Bo
  7. 【转载】图论 500题——主要为hdu/poj/zoj
  8. Canvas入门(2):图形渐变和图像形变换
  9. bzoj 4006 [JLOI2015]管道连接(斯坦纳树+状压DP)
  10. Tkinter教程之Event篇(3)