Skip to content

Instantly share code, notes, and snippets.

@ganeshrn
Created April 23, 2018 18:42
Show Gist options
  • Save ganeshrn/f7401ee0d5f517c2b0653c57713e6ab7 to your computer and use it in GitHub Desktop.
Save ganeshrn/f7401ee0d5f517c2b0653c57713e6ab7 to your computer and use it in GitHub Desktop.
Aruba terminal regex fix
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[?25hHP-2910al# [?25hE[?25h[?25hHP-2910al# [?25h"
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