Created
April 23, 2018 18:42
-
-
Save ganeshrn/f7401ee0d5f517c2b0653c57713e6ab7 to your computer and use it in GitHub Desktop.
Aruba terminal regex fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
ansi_re = [ | |
# see ECMA-48 Section 5.4 (Control Sequences) | |
re.compile(br'((?:\x9b|\x1b\x5b)[\x30-\x3f]*[\x20-\x2f]*[\x40-\x7e])'), | |
re.compile(br'\x08.') | |
] | |
terminal_stdout_re = [ | |
re.compile(br"[\r\n]?[\w]*\(.+\) ?#(?:\s*)(.*)$"), | |
re.compile(br"[pP]assword:$"), | |
re.compile(br"(?<=\s)[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?\s*#\s*$"), | |
re.compile(br"[\r\n]?[\w\+\-\.:\/\[\]]+(?:\([^\)]+\)){0,3}(?:[>#]) ?.*$") | |
] | |
def _strip(data): | |
''' | |
Removes ANSI codes from device response | |
''' | |
for regex in ansi_re: | |
data = regex.sub(b'', data) | |
return data | |
def find_prompt(resp): | |
for regex in terminal_stdout_re: | |
match = regex.search(resp) | |
if match: | |
print("regex matched") | |
print(regex.pattern) | |
return match.group() | |
print("regex not matched") | |
response = ";1H[24;1H[2K[24;1H[?25h[24;1H[24;1HHP-2910al# [24;1H[24;12H[24;1H[?25h[24;12H[24;0HE[24;1H[24;12H[24;1H[2K[24;1H[?25h[24;1H[1;24r[24;1H[1;24r[24;1H[24;1H[2K[24;1H[?25h[24;1H[24;1HHP-2910al# [24;1H[24;12H[24;1H[?25h[24;12H" | |
sanitize_response = _strip(response) | |
print(sanitize_response) | |
prompt = find_prompt(sanitize_response) | |
print(prompt) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment