【问题标题】:'Error opening terminal: unknown.' error when running a command in SSH server through Python“错误打开终端:未知。”通过 Python 在 SSH 服务器中运行命令时出错
【发布时间】: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


    【解决方案1】:

    你的命令需要terminal emulation

    要么:

    1. 尝试查找是否有运行命令的方法,以便它不需要终端仿真。也许-bg switch 可以提供帮助。

      这可能是旧版本 SIPP 中的错误。确保您拥有最新版本。见Startup failure when running from environment without TERM

    2. 或者,启用终端仿真(可能会带来不必要的副作用)。对于 Paramiko SSHClient.exec_command,使用它的 get_pty 参数:

      stdin, stdout, stderr = self.client.exec_command(command, get_pty=True)
      

    【讨论】:

    • 1.我不相信有办法以任何其他方式运行 sipp 命令。这是一个非常小众的命令,据我所知,这是运行它的唯一方法。 2. 我必须在周一回到公司 VPN 后尝试一下,然后我会更新此评论并接受答案。谢谢!
    • 您有最新版本的 SIPP 吗?请参阅我的更新答案。
    • 我确实拥有最新版本的 SIPP,并且能够让该程序与您的第二个选项一起使用。非常感谢您的帮助!
    猜你喜欢
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-04
    • 2013-04-20
    • 2017-01-22
    相关资源
    最近更新 更多