ansible / ansible/pylibssh

Issues with truncated output on MikroTik devices

Open
#256 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
Cython
Stars
79
Forks
47
Avg merge
1d 11h
Merged PRs (30d)
6

Description

##### 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...

```paste below
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\na@192.168.1.1'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 " "...
```paste below
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 \r '
```

Putty raw output...
```
b"=~=~=~=~=~=~=~=~=~=~=~= PuTTY log 2021.09.24 17:31:15 =~=~=~=~=~=~=~=~=~=~=~=\r\nlogin as: a\r\na@192.168.1.1'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.

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with the provided Python script, focusing on Session.invoke_shell() and chan_shell.read_nonblocking(). Trace how the ANSI-colored MikroTik prompt is read and processed when it exceeds 42 characters. Done means the complete prompt is preserved instead of losing its beginning.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.