Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with truncated output on MikroTik devices #256

Open
BaTnz opened this issue Sep 24, 2021 · 1 comment
Open

Issues with truncated output on MikroTik devices #256

BaTnz opened this issue Sep 24, 2021 · 1 comment

Comments

@BaTnz
Copy link

BaTnz commented Sep 24, 2021

SUMMARY

When connecting to a MikroTik device (may also be caused with other devices) the output becomes truncated when the device prompt is grater than 42 characters. The device prompt is "[username@hostname] > " and it colored with a bunch of ANSI escape codes. The issue can be created when the username + hostname is over 35 characters in total. The 42 character count I refer to does not include any ANSI escape codes, this is with them all removed.

ISSUE TYPE
  • Bug Report
PYLISSH and LIBSSH VERSION
pylibsshext v0.2.0 with libssh v0.9.3
OS / ENVIRONMENT

Debian 10.6

STEPS TO REPRODUCE

Can readily be recreated by using pylibsshext to connect to the device directly and reading the output...

import time
from pylibsshext.errors import LibsshSessionException
from pylibsshext.session import Session

ssh = Session()
HOST = 'host'
USER = 'user'
PASSWORD = 'pass'
TIMEOUT = 30
PORT = 22

ssh.connect(
    host=HOST,
    user=USER,
    password=PASSWORD,
    timeout=TIMEOUT,
    port=PORT,
)

chan_shell = ssh.invoke_shell()

cont = True
data = b""

while cont:
    time.sleep(5)
    n_data = chan_shell.read_nonblocking()
    if n_data == b"":
        cont = False
    else:
        data = data + n_data

print(data)
EXPECTED RESULTS

Output with 42 character prompt, correctly shows as "[a@CDEFGHIJKLMNOPQRSTUVWXYZ0123456789A] > "...

b'\r\r\n\r\r\n\r\r\n\r\r\n\r\r\n\r\r\n\r\r\n\r\n\r  MMM      MMM       KKK                          TTTTTTTTTTT      KKK\r\n\r  MMMM    MMMM       KKK                          TTTTTTTTTTT      KKK\r\n\r  MMM MMMM MMM  III  KKK  KKK  RRRRRR     OOOOOO      TTT     III  KKK  KKK\r\n\r  MMM  MM  MMM  III  KKKKK     RRR  RRR  OOO  OOO     TTT     III  KKKKK\r\n\r  MMM      MMM  III  KKK KKK   RRRRRR    OOO  OOO     TTT     III  KKK KKK\r\n\r  MMM      MMM  III  KKK  KKK  RRR  RRR   OOOOOO      TTT     III  KKK  KKK\r\n\r\r\n\r  MikroTik RouterOS 6.47.1 (c) 1999-2020       http://www.mikrotik.com/\r\n\r\r\n[?]             Gives the list of available commands\r\n\rcommand [?]     Gives help on the command and list of arguments\r\n\r\r\n\r[Tab]           Completes the command/word. If the input is ambiguous,\r\n\r                a second [Tab] gives possible options\r\n\r\r\n\r/               Move up to base level\r\n\r..              Move up one level\r\n\r/command        Use command at the base level\r\n\r\x1b[9999B\r\x1b[9999B\x1bZ  \x1b[6n\r\r\r\r[a@CDEFGHIJKLMNOPQRSTUVWXYZ0123456789A] >                                      \r[a@CDEFGHIJKLMNOPQRSTUVWXYZ0123456789A] > '

Putty raw output....

b"=~=~=~=~=~=~=~=~=~=~=~= PuTTY log 2021.09.24 17:28:42 =~=~=~=~=~=~=~=~=~=~=~=\r\nlogin as: a\r\[email protected]'s password: \r\n\r\r\n\r\r\n\r\r\n\r\r\n\r\r\n\r\r\n\r\r\n\r\n\r  MMM      MMM       KKK                          TTTTTTTTTTT      KKK\r\n\r  MMMM    MMMM       KKK                          TTTTTTTTTTT      KKK\r\n\r  MMM MMMM MMM  III  KKK  KKK  RRRRRR     OOOOOO      TTT     III  KKK  KKK\r\n\r  MMM  MM  MMM  III  KKKKK     RRR  RRR  OOO  OOO     TTT     III  KKKKK\r\n\r  MMM      MMM  III  KKK KKK   RRRRRR    OOO  OOO     TTT     III  KKK KKK\r\n\r  MMM      MMM  III  KKK  KKK  RRR  RRR   OOOOOO      TTT     III  KKK  KKK\r\n\r\r\n\r  MikroTik RouterOS 6.47.1 (c) 1999-2020       http://www.mikrotik.com/\r\n\r\r\n[?]             Gives the list of available commands\r\n\rcommand [?]     Gives help on the command and list of arguments\r\n\r\r\n\r[Tab]           Completes the command/word. If the input is ambiguous,\r\n\r                a second [Tab] gives possible options\r\n\r\r\n\r/               Move up to base level\r\n\r..              Move up one level\r\n\r/command        Use command at the base level\r\n\r\x1b[9999B\r\x1b[9999B\x1bZ  \x1b[6n\x1b[4l\x1b[20l\x1b[?47l\x1b[?7h\x1b[?5l\x1b[?25h\x1b[H\x1b[9999B\x1b[6n\x1b[H\x1b[9999B\x1bD\x1b[9999A\x1b[6n\x1b[H\x1b[9999C\x1b[6n\x1b[H\xc4\x9bH\x1b[6n\r   \x1b[H\x1b[9999C\x1b[6n \x1b[6n \x1b[6n\x1b[3;5r\x1b[H\x1b[6n\n\n\n\n\n\n\n\x1b[6n\x1b[9999B\x1b[6n\x1b[r\x1b[1;9999r\r\r\r\x1b[9999B[\x1b[m\x1b[36ma\x1b[m@\x1b[m\x1b[32mCDEFGHIJKLMNOPQRSTUVWXYZ0123456789A\x1b[m] > \r\n\r\r\r\r"
ACTUAL RESULTS

Output with 43 character prompt, should display as "[a@CDEFGHIJKLMNOPQRSTUVWXYZ0123456789AB] > " but shows as "<CDEFGHIJKLMNOPQRSTUVWXYZ0123456789AB] > "...

b'\r\r\n\r\r\n\r\r\n\r\r\n\r\r\n\r\r\n\r\r\n\r\n\r  MMM      MMM       KKK                          TTTTTTTTTTT      KKK\r\n\r  MMMM    MMMM       KKK                          TTTTTTTTTTT      KKK\r\n\r  MMM MMMM MMM  III  KKK  KKK  RRRRRR     OOOOOO      TTT     III  KKK  KKK\r\n\r  MMM  MM  MMM  III  KKKKK     RRR  RRR  OOO  OOO     TTT     III  KKKKK\r\n\r  MMM      MMM  III  KKK KKK   RRRRRR    OOO  OOO     TTT     III  KKK KKK\r\n\r  MMM      MMM  III  KKK  KKK  RRR  RRR   OOOOOO      TTT     III  KKK  KKK\r\n\r\r\n\r  MikroTik RouterOS 6.47.1 (c) 1999-2020       http://www.mikrotik.com/\r\n\r\r\n[?]             Gives the list of available commands\r\n\rcommand [?]     Gives help on the command and list of arguments\r\n\r\r\n\r[Tab]           Completes the command/word. If the input is ambiguous,\r\n\r                a second [Tab] gives possible options\r\n\r\r\n\r/               Move up to base level\r\n\r..              Move up one level\r\n\r/command        Use command at the base level\r\n\r\x1b[9999B\r\x1b[9999B\x1bZ  \x1b[6n\r\r\r\r<CDEFGHIJKLMNOPQRSTUVWXYZ0123456789AB] >                                       \r<CDEFGHIJKLMNOPQRSTUVWXYZ0123456789AB] > '

Putty raw output...

b"=~=~=~=~=~=~=~=~=~=~=~= PuTTY log 2021.09.24 17:31:15 =~=~=~=~=~=~=~=~=~=~=~=\r\nlogin as: a\r\[email protected]'s password: \r\n\r\r\n\r\r\n\r\r\n\r\r\n\r\r\n\r\r\n\r\r\n\r\n\r  MMM      MMM       KKK                          TTTTTTTTTTT      KKK\r\n\r  MMMM    MMMM       KKK                          TTTTTTTTTTT      KKK\r\n\r  MMM MMMM MMM  III  KKK  KKK  RRRRRR     OOOOOO      TTT     III  KKK  KKK\r\n\r  MMM  MM  MMM  III  KKKKK     RRR  RRR  OOO  OOO     TTT     III  KKKKK\r\n\r  MMM      MMM  III  KKK KKK   RRRRRR    OOO  OOO     TTT     III  KKK KKK\r\n\r  MMM      MMM  III  KKK  KKK  RRR  RRR   OOOOOO      TTT     III  KKK  KKK\r\n\r\r\n\r  MikroTik RouterOS 6.47.1 (c) 1999-2020       http://www.mikrotik.com/\r\n\r\r\n[?]             Gives the list of available commands\r\n\rcommand [?]     Gives help on the command and list of arguments\r\n\r\r\n\r[Tab]           Completes the command/word. If the input is ambiguous,\r\n\r                a second [Tab] gives possible options\r\n\r\r\n\r/               Move up to base level\r\n\r..              Move up one level\r\n\r/command        Use command at the base level\r\n\r\x1b[9999B\r\x1b[9999B\x1bZ  \x1b[6n\x1b[4l\x1b[20l\x1b[?47l\x1b[?7h\x1b[?5l\x1b[?25h\x1b[H\x1b[9999B\x1b[6n\x1b[H\x1b[9999B\x1bD\x1b[9999A\x1b[6n\x1b[H\x1b[9999C\x1b[6n\x1b[H\xc4\x9bH\x1b[6n\r   \x1b[H\x1b[9999C\x1b[6n \x1b[6n \x1b[6n\x1b[3;5r\x1b[H\x1b[6n\n\n\n\n\n\n\n\x1b[6n\x1b[9999B\x1b[6n\x1b[r\x1b[1;9999r\r\r\r\x1b[9999B[\x1b[m\x1b[36ma\x1b[m@\x1b[m\x1b[32mCDEFGHIJKLMNOPQRSTUVWXYZ0123456789AB\x1b[m] > \r\n\r\r\r\r"

As you can see when the prompt is too large the beginning of the prompt gets removed. In both cases a bunch of ANSI characters are removed, but when the prompt is over 42 characters some of the prompt is truncated.

I imagine this might have something to do with the fact the prompt starts with "[" which is also used as part of ANSI character escape sequences, but in both instances they are colored in the same fashion, the only different is the character length.

@Jakuje
Copy link
Contributor

Jakuje commented Aug 30, 2024

I do not think this is an pylibsh issue, but a truncation done by the remote shell based on the terminal size. In putty you have the specific terminal emulator, but in case of pylibssh, invoking just the shell does not do any tty/pty allocation unless you get to request_pty_size().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants