Skip to content

Commit

Permalink
Define CLI patterns centrally (telekom#3)
Browse files Browse the repository at this point in the history
* update telnet.py

* update telnet.py

* update ssh.py

* update telnet.py

* update telnet.py

* update telnet.py

* update telnet.py

* update telnet.py

* match for initial command as well

* define cli patterns centrally

* comment

---------
  • Loading branch information
phoenixszg authored Jun 22, 2023
1 parent 95cb9b8 commit 3b140d3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
7 changes: 7 additions & 0 deletions mb_netmgmt/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ def save_key(self, proxy):
def get_proxy(self, stub):
return stub["responses"][0].get("proxy")

def get_cli_patterns(self):
patterns = []
patterns.append(b"[\r\n\x1b\[K][\-\w+\.:/]+(?:\([^\)]+\))?[>#] ?$") # based on IOS driver of Exscript
patterns.append(b"[\r\n\x00\x1b\[K]RP/\d+/(?:RS?P)?\d+\/CPU\d+:[^#]+(?:\([^\)]+\))?#$") # based on IOS XR driver of Exscript
patterns.append(b"[\r\n\x00\x1b\[K](?P<text>[\w/ .:,\(\)\-\?]*)(?P<default>\[[\w/.:\-]*\])?(?(default)(?P<end1>(?:\?|: ?|)$)|(?P<end2>: $))") # Interactive prompt
patterns.append(b"[\r\n\x00\x1b\[K] --More-- $") # Terminal paging
return patterns

def disable_algorithms(disabled_algorithms):
# https://github.com/ncclient/ncclient/issues/526#issuecomment-1096563028
Expand Down
10 changes: 3 additions & 7 deletions mb_netmgmt/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,9 @@ def respond(self, response, request_id):
return response

def read_proxy_response(self):
patterns = []
patterns.append(self.channel.command_prompt) # Initial command prompt
patterns.append(b"[\r\n\x1b\[K][\-\w+\.:/]+(?:\([^\)]+\))?[>#] ?$") # IOS
patterns.append(b"[\r\n\x00\x1b\[K]RP/\d+/(?:RS?P)?\d+\/CPU\d+:[^#]+(?:\([^\)]+\))?#$") # IOS XR
patterns.append(b"[\r\n\x00\x1b\[K](?P<text>[\w/ .:,\(\)\-\?]*)(?P<default>\[[\w/.:\-]*\])?(?(default)(?P<end1>(?:\?|: ?|)$)|(?P<end2>: $))") # Interactive prompt
patterns.append(b"[\r\n\x00\x1b\[K] --More-- $") # Terminal paging
message = self.read_message(self.channel.upstream, patterns)
prompt_patterns = [self.channel.command_prompt]
prompt_patterns += self.get_cli_patterns()
message = self.read_message(self.channel.upstream, prompt_patterns)
return {"response": message.decode()}

def read_message(self, channel, patterns):
Expand Down
8 changes: 2 additions & 6 deletions mb_netmgmt/telnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,8 @@ def respond(self, response, request_id):
self.stopped = True

def read_proxy_response(self):
prompt_patterns = []
prompt_patterns.append(self.command_prompt) # Initial command prompt
prompt_patterns.append(b"[\r\n][\-\w+\.:/]+(?:\([^\)]+\))?[>#] ?$") # IOS
prompt_patterns.append(b"[\r\n\x00]RP/\d+/(?:RS?P)?\d+\/CPU\d+:[^#]+(?:\([^\)]+\))?#$") # IOS XR
prompt_patterns.append(b"[\r\n\x00](?P<text>[\w/ .:,\(\)\-\?]*)(?P<default>\[[\w/.:\-]*\])?(?(default)(?P<end1>(?:\?|: ?|)$)|(?P<end2>: $))") # Interactive prompt
prompt_patterns.append(b"[\r\n\x00] --More-- $") # Terminal paging
prompt_patterns = [self.command_prompt]
prompt_patterns += self.get_cli_patterns()
_, _, response = self.telnet.expect(prompt_patterns)
return {"response": response.decode()}

Expand Down

0 comments on commit 3b140d3

Please sign in to comment.