From bda36bb5f16fefab4a45ec871940175ebcf9e9e4 Mon Sep 17 00:00:00 2001 From: Carl Montanari Date: Mon, 27 May 2024 13:31:24 -0700 Subject: [PATCH] feat: further expanded asni pattern stuff --- scrapli/channel/base_channel.py | 8 +++++--- tests/unit/channel/test_base_channel.py | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/scrapli/channel/base_channel.py b/scrapli/channel/base_channel.py index afcee354..5ba25216 100644 --- a/scrapli/channel/base_channel.py +++ b/scrapli/channel/base_channel.py @@ -12,13 +12,15 @@ from scrapli.transport.base import AsyncTransport, Transport ANSI_ESCAPE_PATTERN = re.compile( - pattern=rb"[\x1B\x9B\x9D](\s)?" # Prefix ESC (^) or CSI (^[) or OSC (^]) + pattern=rb"[\x1B\x9B\x9D](\s)?" # Prefix ESC (^) or CSI (^[) or OSC (^) rb"(" rb"([78ME])" # control cursor position rb"|" - rb"((\]\d).*[\x07])" # BEL (Terminal bell) + rb"((\]\d).*?[\x07])" # BEL (Terminal bell) rb"|" - rb"(\[[{}();#=?0-9]*[A-Zhglnmsu~])" # control codes starts with `[` e.x. ESC [2;37;41m + rb"(\[.*?[@-~])" # control codes starts with `[` e.x. ESC [2;37;41m + rb"|" + rb"(\[.*?[0-9;]m)" # Select Graphic Rendition (SGR) control sequence rb")", flags=re.VERBOSE, ) diff --git a/tests/unit/channel/test_base_channel.py b/tests/unit/channel/test_base_channel.py index 7bd333d5..ffd381e8 100644 --- a/tests/unit/channel/test_base_channel.py +++ b/tests/unit/channel/test_base_channel.py @@ -353,6 +353,11 @@ def test_process_output(base_channel): b"\x1b[?2004h\x1b]0;user@line5-cpe-0: ~\x07user@line5-cpe-0:~$", b"user@line5-cpe-0:~$", ), + # https://github.com/carlmontanari/scrapli/issues/331 + ( + 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", + b"Your previous successful login (as manager) was on 2024-05-24 11:29:02 \n from X.X.X.X\nHOSTNAME# HOSTNAME# ", + ), ), ) def test_strip_ansi(base_channel, buf: bytes, expected: bytes):