[英]'Error opening terminal: unknown.' error when running a command in SSH server through Python
我正在尝试使用 Python 通过 SSH 连接到服务器,并且我已经成功地做到了。 我能够在 Python 中成功运行命令,但有一个例外,主要命令是我的程序的重点。 这是一个 SIPp 命令,只会在 SSH 服务器和特定文件夹中运行。
当我在终端中运行该命令时,它运行良好; 然而,当我通过 PExpect 或 Paramiko 连接到 SSH 服务器时(两者都工作正常),我尝试发送我的命令,但我得到了
打开终端时出错:未知
到目前为止,我阅读了文档,尝试使用操作系统、子进程以及多种不同的方式与 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]")
两者都给我这个错误:打开终端时出错:未知。
我的猜测是它没有产生实际的终端,但此时我不知道该怎么做。 任何帮助将不胜感激
您的命令需要终端仿真。
任何一个:
尝试查找是否有一种方法可以运行该命令,以便它不需要终端仿真。 也许-bg
开关可以提供帮助。
可能这是旧版本 SIPP 中的错误。 确保您拥有最新版本。 请参阅从没有 TERM 的环境中运行时启动失败。
或者,启用终端仿真(什么会带来不必要的副作用)。 使用SSHClient.exec_command
,使用其get_pty
参数:
stdin, stdout, stderr = self.client.exec_command(command, get_pty=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.