Search code examples
pythonsshterminalparamiko

'Error opening terminal: unknown.' error when running a command in SSH server through Python


I am trying to SSH to a server with Python and I have been able to do so successfully. I am able to run the commands within Python successfully with one exception, the main command that is the focus of my program. It is a SIPp command that will only run within the SSH server and in a specific folder.

When I run the command in my terminal, it works perfectly fine; however, when I connect to the SSH server through PExpect or Paramiko (both work fine), I try to send my command but I get the

Error Opening Terminal: Unknown

I have so far, read the docs, tried using os, subprocess, and multiple different ways of connecting with Paramiko and Pxssh. The several people I work with were not able to figure it out either.

The SIPp command that I am trying to send and read the output of:

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

Connecting to SSH through Pxssh (PExpect):

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)


Connecting to SSH through Paramiko:

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]")

Both give me this error: Error opening terminal: unknown.

My guess is that it is not spawning an actual terminal but I can't figure out what to do at this point. Any help would be sincerely appreciated


Solution

  • Your command needs terminal emulation.

    Either:

    1. Try to find if there's a way to run the command, so that it does not require the terminal emulation. Maybe -bg switch can help.

      Possibly this was a bug in an older version of SIPP. Make sure you have the latest version. See Startup failure when running from environment without TERM.

    2. Or, enable the terminal emulation (what can bring unwanted side effects). With Paramiko SSHClient.exec_command, use its get_pty argument:

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