scrapli
scrapli copied to clipboard
ANSI escape code not stripped from Aruba 2530 series switches
Describe the bug Trying to add a community driver for Aruba (former HP) 2530 series switches (https://www.hpe.com/psnow/doc/c04111414), but I'm encountering issues with some ANSI escape codes not being stripped from the channel input, and thus pattern matching failing (authentication successful check, prompt detection, etc.).
For some reason, it seems like the switch outputs a lot of escape sequences, for example immediately after login succeeds:
read: b'\x1b[?6l\x1b[1;80r\x1b[?7h\x1b[2J\x1b[1;1H\x1b[1920;1920H\x1b[6n\x1b[1;1HYour previous successful login (as manager) was on 2024-05-24 11:29:02 \n from X.X.X.X\n\x1b[1;80r\x1b[80;1H\x1b[80;1H\x1b[2K\x1b[80;1H\x1b[?25h\x1b[80;1H\x1b[80;1HHOSTNAME# \x1b[80;1H\x1b[80;20H\x1b[80;1H\x1b[?25h\x1b[80;20H\x1b[1;0H\x1b[1M\x1b[80;1H\x1b[1L\x1b[80;20H\x1b[80;1H\x1b[2K\x1b[80;1H\x1b[?25h\x1b[80;1H\x1b[1;80r\x1b[80;1H\x1b[1;80r\x1b[80;1H\x1b[80;1H\x1b[2K\x1b[80;1H\x1b[?25h\x1b[80;1H\x1b[80;1HHOSTNAME# \x1b[80;1H\x1b[80;20H\x1b[80;1H\x1b[?25h\x1b[80;20H'
Even after BaseChannel._strip_ansi()
is being called internally by scrapli, I'm still left with a few escape sequences in the output:
b'\x1b[1;80rYour previous successful login (as manager) was on 2024-05-24 11:29:02 \n from X.X.X.X\n\x1b[1;80rHOSTNAME# \x1b[1;80r\x1b[1;80rHOSTNAME# '
I assume that base_channel.ANSI_ESCAPE_PATTERN
is missing patterns for these escape codes, however I can't exactly figure out what these codes do. Maybe someone can chime in on this.
I tried manually altering the regex pattern being used internally by scrapli and replacing it with one of the answers mentioned in this Stack Overflow thread, and it seems like all escape sequences get stripped (and I am left with a clean output which matches my regex patterns for prompt matching):
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
result = ansi_escape.sub('', sometext)
To Reproduce Steps to reproduce the behavior:
-
Your script Can't really provide a sample script yet, since I'm still trying to piece the community driver together (and currently failing to make it work due to this issue). However, it is based on
GenericDriver
. -
What you're connecting to (vendor, platform, version) Aruba (HP/HPE) ProCurve 2530-24G switches, running OS version YA.16.10.0009.
-
Anything else relevant -
Expected behavior
After calling BaseChannel._strip_ansi()
, the output should probably be similar to this:
Your previous successful login (as manager) was on 2024-05-24 11:29:02 \n from X.X.X.X\nHOSTNAME# HOSTNAME#
Stack Trace -
Screenshots -
OS (please complete the following information):
- OS: Docker container based on
python:3.11
. - scrapli version:
scrapli==2024.1.30 & scrapli-community==2024.1.30
Additional context -