'Error opening terminal: unknown.' 当通过 Python 在 SSH 服务器中执行 运行 命令时出错
'Error opening terminal: unknown.' error when running a command in SSH server through Python
我正在尝试使用 Python 通过 SSH 连接到服务器,并且我已经成功完成了。除了一个例外,我能够 运行 Python 中的命令成功,主命令是我程序的重点。这是一个 SIPp 命令,只会在 SSH 服务器和特定文件夹中 运行。
当我在我的终端中 运行 命令时,它工作得很好;然而,当我通过 PExpect 或 Paramiko 连接到 SSH 服务器时(两者都工作正常),我尝试发送我的命令但我得到
Error Opening Terminal: Unknown
到目前为止,我已经阅读了文档,尝试使用 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]")
两者都给我这个错误:打开终端时出错:未知。
我的猜测是它没有产生实际的终端,但我现在不知道该怎么做。任何帮助将不胜感激
您的命令需要 terminal emulation.
或者:
尝试寻找是否有办法 运行 命令,这样它就不需要终端仿真。也许 -bg
switch 可以提供帮助。
这可能是旧版本 SIPP 中的错误。确保您拥有最新版本。见 Startup failure when running from environment without TERM.
或者,启用终端仿真(这会带来不必要的副作用)。对于 Paramiko SSHClient.exec_command
,使用其 get_pty
参数:
stdin, stdout, stderr = self.client.exec_command(command, get_pty=True)
我正在尝试使用 Python 通过 SSH 连接到服务器,并且我已经成功完成了。除了一个例外,我能够 运行 Python 中的命令成功,主命令是我程序的重点。这是一个 SIPp 命令,只会在 SSH 服务器和特定文件夹中 运行。
当我在我的终端中 运行 命令时,它工作得很好;然而,当我通过 PExpect 或 Paramiko 连接到 SSH 服务器时(两者都工作正常),我尝试发送我的命令但我得到
Error Opening Terminal: Unknown
到目前为止,我已经阅读了文档,尝试使用 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]")
两者都给我这个错误:打开终端时出错:未知。
我的猜测是它没有产生实际的终端,但我现在不知道该怎么做。任何帮助将不胜感激
您的命令需要 terminal emulation.
或者:
尝试寻找是否有办法 运行 命令,这样它就不需要终端仿真。也许
-bg
switch 可以提供帮助。这可能是旧版本 SIPP 中的错误。确保您拥有最新版本。见 Startup failure when running from environment without TERM.
或者,启用终端仿真(这会带来不必要的副作用)。对于 Paramiko
SSHClient.exec_command
,使用其get_pty
参数:stdin, stdout, stderr = self.client.exec_command(command, get_pty=True)