【发布时间】:2019-11-22 16:16:13
【问题描述】:
我正在尝试使用 Python SSH 到服务器,并且我已经能够成功地做到这一点。我能够在 Python 中成功运行命令,但有一个例外,即作为我程序重点的主要命令。它是一个 SIPp 命令,只能在 SSH 服务器和特定文件夹中运行。
当我在终端中运行该命令时,它运行良好;但是,当我通过 PExpect 或 Paramiko 连接到 SSH 服务器时(两者都工作正常),我尝试发送我的命令,但我得到了
错误打开终端:未知
到目前为止,我已经阅读了文档,尝试使用 os、子进程以及与 Paramiko 和 Pxssh 连接的多种不同方式。与我一起工作的几个人也无法弄清楚。
我尝试发送的 SIPp 命令并读取以下输出:
sipp -r 5 -m 20 -trace_msg -inf users.csv -sf register.xml -d 10000 -i [IP addresses]
# some of the command was left out for simplicity's sake
# there is no issue with the command
通过 Pxssh (PExpect) 连接到 SSH:
from pexpect import pxssh
from getpass import getpass
try:
s = pxssh.pxssh()
hostname = input('hostname: ')
username = input('username: ')
password = getpass("password :", None)
s.login(hostname, username, password)
s.sendline('cd [location of the folder]')
s.prompt()
print(s.before)
s.sendline('sipp -r 5 -m 20 -trace_msg -inf users.csv -sf register.xml -d 10000 -i [IP addresses]') #this is the only line that doesn't work / output anything.
s.prompt()
print(s.before)
s.sendline('ls')
s.prompt()
print(s.before)
s.logout()
except pxssh.ExceptionPxssh as e:
print("Something went wrong. Try again with the correct Host Name, Username, and Password")
print(e)
通过 Paramiko 连接到 SSH:
from paramiko import client
from getpass import getpass
class ssh:
client = None
def __init__(self, address, username, password):
self.client = client.SSHClient()
self.client.set_missing_host_key_policy(client.AutoAddPolicy())
self.client.connect(address, username=username, password=password, look_for_keys=False)
def sendCommand(self, command):
if self.client:
stdin, stdout, stderr = self.client.exec_command(command)
output = stdout.readlines()
print(output, stderr.readlines())
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
alldata = stdout.channel.recv(1024)
prevdata = b"1"
while prevdata:
prevdata = stdout.channel.recv(1024)
alldata += prevdata
print(str(alldata, "utf8"))
self.client.close()
else:
print("Connection not opened.")
connection = ssh([ssh info])
connection.sendCommand("cd [location] ; sipp -r 5 -m 20 -trace_msg -inf users.csv -sf register.xml -d 10000 -i [IP addresses]")
两者都给我这个错误:打开终端时出错:未知。
我的猜测是它并没有产生一个实际的终端,但我现在不知道该怎么做。任何帮助将不胜感激
【问题讨论】:
标签: python ssh terminal paramiko