0

I am having trouble with understanding this regular expression that exscript is using to detect the prompt for an NX-OS switch

'[\\\\r\\\\n][\\\\-\\\\w+\\\\.:/]+(?:\\\\([^\\\\)]+\\\\))?[>#] ?$'

This is Python

My prompt should be something like a string of any number of alpha numeric characters that ends with '#'

I tried '^.*#' but it did not work. There is no spaces or - in the string

Edit

For your fan here are a couple of other similar regex used in the login process to recognize various prompts that the remote OS can send back

generic: waiting for: ['[\\r\\n][^\\r\\n]*(?:bad secrets|denied|invalid|too short|incorrect|connection timed out|failed|failure)', '(user ?name|user|login): *$', '(?:s\\/key|otp-md4) (\\d+) (\\S+)(?=\\s|[\\r\\n])', 'password:? *$', '[\\r\\n](?:[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\\!\\"\\#\\$\\%\\&\\\'\\(\\)\\*\\+\\,\\-\\.\\/\\:\\;\\<\\=\\>\\?\\@\\[\\\\\\]\\^\\_\\`\\{\\|\\}\\~\\ \\\t\\\n\\\r\\\x0b\\\x0c]*|[\\x1b\\x07\\x00]*)[\\[\\<]?\\w+(?:(?:(?:[\\w+\\-]+)\\@)?(?:[\\w+\\-\\.]+))?:?(?:(?:(?:(?:[\\w\\+\\-\\._]+))?(?:/(?:[\\w\\+\\-\\._]+))*/?)|~(?:(?:(?:[\\w\\+\\-\\._]+))?(?:/(?:[\\w\\+\\-\\._]+))*/?)?)?[: ]?(?:(?:(?:(?:[\\w\\+\\-\\._]+))?(?:/(?:[\\w\\+\\-\\._]+))*/?)|~(?:(?:(?:[\\w\\+\\-\\._]+))?(?:/(?:[\\w\\+\\-\\._]+))*/?)?)?(?:\\((?:[\\w\\+\\-\\._]+)\\))?[\\]\\-]?[#>%\\$\\]] ?[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\\!\\"\\#\\$\\%\\&\\\'\\(\\)\\*\\+\\,\\-\\.\\/\\:\\;\\<\\=\\>\\?\\@\\[\\\\\\]\\^\\_\\`\\{\\|\\}\\~\\ \\\t\\\n\\\r\\\x0b\\\x0c]*\\Z']
generic: Expecting a prompt
generic: Expected pattern: ["'[\\\\r\\\\n][^\\\\r\\\\n]*(?:bad secrets|denied|invalid|too short|incorrect|connection timed out|failed|failure)'", "'(user ?name|user|login): *$'", "'(?:s\\\\/key|otp-md4) (\\\\d+) (\\\\S+)(?=\\\\s|[\\\\r\\\\n])'", "'password:? *$'", '\'[\\\\r\\\\n](?:[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\\\\!\\\\"\\\\#\\\\$\\\\%\\\\&\\\\\\\'\\\\(\\\\)\\\\*\\\\+\\\\,\\\\-\\\\.\\\\/\\\\:\\\\;\\\\<\\\\=\\\\>\\\\?\\\\@\\\\[\\\\\\\\\\\\]\\\\^\\\\_\\\\`\\\\{\\\\|\\\\}\\\\~\\\\ \\\\\\t\\\\\\n\\\\\\r\\\\\\x0b\\\\\\x0c]*|[\\\\x1b\\\\x07\\\\x00]*)[\\\\[\\\\<]?\\\\w+(?:(?:(?:[\\\\w+\\\\-]+)\\\\@)?(?:[\\\\w+\\\\-\\\\.]+))?:?(?:(?:(?:(?:[\\\\w\\\\+\\\\-\\\\._]+))?(?:/(?:[\\\\w\\\\+\\\\-\\\\._]+))*/?)|~(?:(?:(?:[\\\\w\\\\+\\\\-\\\\._]+))?(?:/(?:[\\\\w\\\\+\\\\-\\\\._]+))*/?)?)?[: ]?(?:(?:(?:(?:[\\\\w\\\\+\\\\-\\\\._]+))?(?:/(?:[\\\\w\\\\+\\\\-\\\\._]+))*/?)|~(?:(?:(?:[\\\\w\\\\+\\\\-\\\\._]+))?(?:/(?:[\\\\w\\\\+\\\\-\\\\._]+))*/?)?)?(?:\\\\((?:[\\\\w\\\\+\\\\-\\\\._]+)\\\\))?[\\\\]\\\\-]?[#>%\\\\$\\\\]] ?[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\\\\!\\\\"\\\\#\\\\$\\\\%\\\\&\\\\\\\'\\\\(\\\\)\\\\*\\\\+\\\\,\\\\-\\\\.\\\\/\\\\:\\\\;\\\\<\\\\=\\\\>\\\\?\\\\@\\\\[\\\\\\\\\\\\]\\\\^\\\\_\\\\`\\\\{\\\\|\\\\}\\\\~\\\\ \\\\\\t\\\\\\n\\\\\\r\\\\\\x0b\\\\\\x0c]*\\\\Z\'']
generic: Shell prompt received.
generic: Calling driver.auto_authorize().
generic: Attempting to app-authorize usernaem.
generic: waiting for: ['[\\r\\n][^\\r\\n]*(?:bad secrets|denied|invalid|too short|incorrect|connection timed out|failed|failure)', '(user ?name|user|login): *$', '(?:s\\/key|otp-md4) (\\d+) (\\S+)(?=\\s|[\\r\\n])', 'password:? *$', '[\\r\\n](?:[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\\!\\"\\#\\$\\%\\&\\\'\\(\\)\\*\\+\\,\\-\\.\\/\\:\\;\\<\\=\\>\\?\\@\\[\\\\\\]\\^\\_\\`\\{\\|\\}\\~\\ \\\t\\\n\\\r\\\x0b\\\x0c]*|[\\x1b\\x07\\x00]*)[\\[\\<]?\\w+(?:(?:(?:[\\w+\\-]+)\\@)?(?:[\\w+\\-\\.]+))?:?(?:(?:(?:(?:[\\w\\+\\-\\._]+))?(?:/(?:[\\w\\+\\-\\._]+))*/?)|~(?:(?:(?:[\\w\\+\\-\\._]+))?(?:/(?:[\\w\\+\\-\\._]+))*/?)?)?[: ]?(?:(?:(?:(?:[\\w\\+\\-\\._]+))?(?:/(?:[\\w\\+\\-\\._]+))*/?)|~(?:(?:(?:[\\w\\+\\-\\._]+))?(?:/(?:[\\w\\+\\-\\._]+))*/?)?)?(?:\\((?:[\\w\\+\\-\\._]+)\\))?[\\]\\-]?[#>%\\$\\]] ?[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\\!\\"\\#\\$\\%\\&\\\'\\(\\)\\*\\+\\,\\-\\.\\/\\:\\;\\<\\=\\>\\?\\@\\[\\\\\\]\\^\\_\\`\\{\\|\\}\\~\\ \\\t\\\n\\\r\\\x0b\\\x0c]*\\Z']
generic: Expecting a prompt
generic: Expected pattern: ["'[\\\\r\\\\n][^\\\\r\\\\n]*(?:bad secrets|denied|invalid|too short|incorrect|connection timed out|failed|failure)'", "'(user ?name|user|login): *$'", "'(?:s\\\\/key|otp-md4) (\\\\d+) (\\\\S+)(?=\\\\s|[\\\\r\\\\n])'", "'password:? *$'", '\'[\\\\r\\\\n](?:[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\\\\!\\\\"\\\\#\\\\$\\\\%\\\\&\\\\\\\'\\\\(\\\\)\\\\*\\\\+\\\\,\\\\-\\\\.\\\\/\\\\:\\\\;\\\\<\\\\=\\\\>\\\\?\\\\@\\\\[\\\\\\\\\\\\]\\\\^\\\\_\\\\`\\\\{\\\\|\\\\}\\\\~\\\\ \\\\\\t\\\\\\n\\\\\\r\\\\\\x0b\\\\\\x0c]*|[\\\\x1b\\\\x07\\\\x00]*)[\\\\[\\\\<]?\\\\w+(?:(?:(?:[\\\\w+\\\\-]+)\\\\@)?(?:[\\\\w+\\\\-\\\\.]+))?:?(?:(?:(?:(?:[\\\\w\\\\+\\\\-\\\\._]+))?(?:/(?:[\\\\w\\\\+\\\\-\\\\._]+))*/?)|~(?:(?:(?:[\\\\w\\\\+\\\\-\\\\._]+))?(?:/(?:[\\\\w\\\\+\\\\-\\\\._]+))*/?)?)?[: ]?(?:(?:(?:(?:[\\\\w\\\\+\\\\-\\\\._]+))?(?:/(?:[\\\\w\\\\+\\\\-\\\\._]+))*/?)|~(?:(?:(?:[\\\\w\\\\+\\\\-\\\\._]+))?(?:/(?:[\\\\w\\\\+\\\\-\\\\._]+))*/?)?)?(?:\\\\((?:[\\\\w\\\\+\\\\-\\\\._]+)\\\\))?[\\\\]\\\\-]?[#>%\\\\$\\\\]] ?[^0123456789ab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\\\\-]?[#>%\\\\$\\\\]] ?[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\\\\!\\\\"\\\\#\\\\$\\\\%\\\\&\\\\\\\'\\\\(\\\\)\\\\*\\\\+\\\\,\\\\-\\\\.\\\\/\\\\:\\\\;\\\\<\\\\=\\\\>\\\\?\\\\@\\\\[\\\\\\\\\\\\]\\\\^\\\\_\\\\`\\\\{\\\\|\\\\}\\\\~\\\\ \\\\\\t\\\\\\n\\\\\\r\\\\\\x0b\\\\\\x0c]*\\\\Z\'']
 
halfer
  • 19,824
  • 17
  • 99
  • 186
MiniMe
  • 1,057
  • 4
  • 22
  • 47
  • 2
    The escaping looks strange, but if it is OK, you might be looking for something like `'\\\\w+#$'`. But in Python, you'd use `r'\w+$'` – Wiktor Stribiżew Mar 20 '17 at 13:28
  • @WiktorStribiżew the escaping is a python thing with regex... literal backslashes need to be escaped twice with regular strings due to literal evaluation order (before regex call) – Aaron Mar 20 '17 at 13:30
  • \\\\ has no sense for me – MiniMe Mar 20 '17 at 13:31
  • 1
    That's called backslash hell – Thomas Ayoub Mar 20 '17 at 13:31
  • @MiniMe use a raw string to cut the escapes in half... `r"[\\r\\n][\\-\\w+\\.:/]+(?:\\([^\\)]+\\))?[>#] ?$"` – Aaron Mar 20 '17 at 13:35
  • @MiniMe does this regex actually work? It seems very broken... – Aaron Mar 20 '17 at 13:43
  • This is what the script spits when I put exscript in verbose mode: nxos: Expected pattern: ["'[\\\\r\\\\n][\\\\-\\\\w+\\\\.:/]+(?:\\\\([^\\\\)]+\\\\))?[>#] ?$'"] I presume that the driver was tested and it does work. I have seen equally complicated patterns for IOS and they work. i just don't understand them – MiniMe Mar 20 '17 at 13:45
  • There are tons of tutorials on regex out there especially for python.. [This](https://regex101.com/) is my favorite website for debugging regex patterns, because it gives a very explicit breakdown of what's going on. take note to select the python tab, and use the raw string instead of the fully escaped one. – Aaron Mar 20 '17 at 13:52
  • I am using https://regex101.com but the above expressions do not make much sense there – MiniMe Mar 20 '17 at 13:54

0 Answers0