import paramiko
import time
import allure
from self import self
import pytest
class SSLConnection:
def run(self, ssh, command):
stdin, stdout, stderr = ssh.exec_command(command)
result = stdout.read().decode()
# print(result)
return result
def shutdown(self, ssh):
ssh.close()
def connectSSL(self, hostname, port, username, password):
# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机,否则可能报错:paramiko.ssh_exception.SSHException: Server '192.168.43.140' not found in known_hosts
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname=hostname, port=port, username=username, password=password)
# 执行命令
# stdin:标准输入(就是你输入的命令);stdout:标准输出(就是命令执行结果);stderr:标准错误(命令执行过程中如果出错了就把错误打到这里),stdout和stderr仅会输出一个
# print(command)
return ssh
# stdin, stdout, stderr = ssh.exec_command(command)
# 获取命令结果
# result = stdout.read().decode() # 这个有问题,不显示错误,可以修改一下,先判断stdout有没有值,如果输出没有,就显示错误
# print(result)
# return result
# 关闭连接
# ssh.close()
@allure.feature("ceshi")
def test_compare():
hostname = '192.168.60.129'
port = '22'
username = 'root'
password = '1'
command = [u'sudo uptime',
'date +%s', 'ifconfig']
ssh = SSLConnection.connectSSL(self, hostname=hostname, port=port, username=username, password=password)
for i in range(0, len(command)):
res = SSLConnection.run(self, ssh=ssh, command=command[i])
print(res)
SSLConnection.shutdown(self, ssh=ssh)
if __name__ == '__main__':
test_compare()
标签:stdout,self,some,pytest,command,ssh,password,port From: https://www.cnblogs.com/luobomao/p/17190066.html