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