From f13f2cc1db6823d1de3c6fe55bd651fee3d7d675 Mon Sep 17 00:00:00 2001 From: Martin Komon Date: Thu, 24 Dec 2015 13:28:18 +0100 Subject: [PATCH 1/2] ignore non-UTF-8 input --- netmiko/base_connection.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/netmiko/base_connection.py b/netmiko/base_connection.py index 00457ddd0..7bc4827f6 100644 --- a/netmiko/base_connection.py +++ b/netmiko/base_connection.py @@ -177,7 +177,7 @@ def establish_connection(self, sleep_time=3, verbose=True, timeout=8, time.sleep(sleep_time) # Strip any initial data if self.remote_conn.recv_ready(): - return self.remote_conn.recv(MAX_BUFFER).decode('utf-8') + return self.remote_conn.recv(MAX_BUFFER).decode('utf-8', 'ignore') else: i = 0 while i <= 10: @@ -185,7 +185,7 @@ def establish_connection(self, sleep_time=3, verbose=True, timeout=8, self.remote_conn.sendall('\n') time.sleep(.5) if self.remote_conn.recv_ready(): - return self.remote_conn.recv(MAX_BUFFER).decode('utf-8') + return self.remote_conn.recv(MAX_BUFFER).decode('utf-8', 'ignore') else: i += 1 return "" @@ -216,7 +216,7 @@ def disable_paging(self, command="terminal length 0\n", delay_factor=.5): time.sleep(1 * delay_factor) # Clear the buffer on the screen - output = self.remote_conn.recv(MAX_BUFFER).decode('utf-8') + output = self.remote_conn.recv(MAX_BUFFER).decode('utf-8', 'ignore') if self.ansi_escape_codes: output = self.strip_ansi_escape_codes(output) @@ -259,7 +259,7 @@ def set_base_prompt(self, pri_prompt_terminator='#', self.clear_buffer() self.remote_conn.sendall("\n") self.wait_for_recv_ready(delay_factor) - prompt = self.remote_conn.recv(MAX_BUFFER).decode('utf-8') + prompt = self.remote_conn.recv(MAX_BUFFER).decode('utf-8', 'ignore') # Some platforms have ANSI escape codes if self.ansi_escape_codes: @@ -302,7 +302,7 @@ def find_prompt(self, delay_factor=.5): self.remote_conn.sendall("\n") time.sleep(1 * delay_factor) - prompt = self.remote_conn.recv(MAX_BUFFER).decode('utf-8') + prompt = self.remote_conn.recv(MAX_BUFFER).decode('utf-8', 'ignore') # Some platforms have ANSI escape codes if self.ansi_escape_codes: @@ -326,7 +326,7 @@ def clear_buffer(self): ''' if self.remote_conn.recv_ready(): - return self.remote_conn.recv(MAX_BUFFER).decode('utf-8') + return self.remote_conn.recv(MAX_BUFFER).decode('utf-8', 'ignore') else: return None @@ -370,7 +370,7 @@ def send_command(self, command_string, delay_factor=.5, max_loops=30, i += 1 # Keep reading data as long as available (up to max_loops) if self.remote_conn.recv_ready(): - output += self.remote_conn.recv(MAX_BUFFER).decode('utf-8') + output += self.remote_conn.recv(MAX_BUFFER).decode('utf-8', 'ignore') else: not_done = False @@ -454,7 +454,7 @@ def send_command_expect(self, command_string, expect_string=None, if self.remote_conn.recv_ready(): if debug: print("recv_ready = True") - output += self.remote_conn.recv(MAX_BUFFER).decode('utf-8') + output += self.remote_conn.recv(MAX_BUFFER).decode('utf-8', 'ignore') if search_pattern in output: break else: From fd844a76474de10a6507fa8d8ed890c6636291b8 Mon Sep 17 00:00:00 2001 From: Martin Komon Date: Thu, 31 Dec 2015 12:43:31 +0100 Subject: [PATCH 2/2] ignore non-UTF-8 input for all vendors --- netmiko/avaya/avaya_ers_ssh.py | 2 +- netmiko/cisco/cisco_asa_ssh.py | 4 ++-- netmiko/cisco/cisco_wlc_ssh.py | 5 ++--- netmiko/fortinet/fortinet_ssh.py | 2 +- netmiko/hp/hp_comware_ssh.py | 2 +- netmiko/huawei/huawei_ssh.py | 2 +- netmiko/juniper/juniper_ssh.py | 2 +- 7 files changed, 9 insertions(+), 10 deletions(-) diff --git a/netmiko/avaya/avaya_ers_ssh.py b/netmiko/avaya/avaya_ers_ssh.py index 9a41bae92..b3f65dc56 100644 --- a/netmiko/avaya/avaya_ers_ssh.py +++ b/netmiko/avaya/avaya_ers_ssh.py @@ -27,7 +27,7 @@ def special_login_handler(self, delay_factor=.5): i = 0 while i <= 12: if self.remote_conn.recv_ready(): - output = self.remote_conn.recv(MAX_BUFFER).decode('utf-8') + output = self.remote_conn.recv(MAX_BUFFER).decode('utf-8', 'ignore') if 'Ctrl-Y' in output: self.remote_conn.sendall(CTRL_Y) if 'sername' in output: diff --git a/netmiko/cisco/cisco_asa_ssh.py b/netmiko/cisco/cisco_asa_ssh.py index cfe3b9be5..33a90e165 100644 --- a/netmiko/cisco/cisco_asa_ssh.py +++ b/netmiko/cisco/cisco_asa_ssh.py @@ -63,12 +63,12 @@ def enable(self, delay_factor=.5): self.remote_conn.sendall("\nenable\n") time.sleep(1 * delay_factor) - output = self.remote_conn.recv(MAX_BUFFER).decode('utf-8') + output = self.remote_conn.recv(MAX_BUFFER).decode('utf-8', 'ignore') if 'password' in output.lower(): self.remote_conn.sendall(self.secret + '\n') self.remote_conn.sendall('\n') time.sleep(1 * delay_factor) - output += self.remote_conn.recv(MAX_BUFFER).decode('utf-8') + output += self.remote_conn.recv(MAX_BUFFER).decode('utf-8', 'ignore') if not self.check_enable_mode(): raise ValueError("Failed to enter enable mode") diff --git a/netmiko/cisco/cisco_wlc_ssh.py b/netmiko/cisco/cisco_wlc_ssh.py index 079301f58..2f00da3d9 100644 --- a/netmiko/cisco/cisco_wlc_ssh.py +++ b/netmiko/cisco/cisco_wlc_ssh.py @@ -29,7 +29,7 @@ def special_login_handler(self, delay_factor=.5): i = 0 while i <= 12: if self.remote_conn.recv_ready(): - output = self.remote_conn.recv(MAX_BUFFER).decode('utf-8') + output = self.remote_conn.recv(MAX_BUFFER).decode('utf-8', 'ignore') if 'login as' in output or 'User' in output: self.remote_conn.sendall(self.username + '\n') elif 'Password' in output: @@ -84,8 +84,7 @@ def send_command_w_enter(self, *args, **kwargs): i += 1 if self.remote_conn.recv_ready(): # Unicode error occurred in output (errors=ignore strips this out). - output += self.remote_conn.recv(MAX_BUFFER).decode('utf-8', - errors='ignore') + output += self.remote_conn.recv(MAX_BUFFER).decode('utf-8', 'ignore') else: not_done = False diff --git a/netmiko/fortinet/fortinet_ssh.py b/netmiko/fortinet/fortinet_ssh.py index 0c5b1a855..0c12a1c51 100644 --- a/netmiko/fortinet/fortinet_ssh.py +++ b/netmiko/fortinet/fortinet_ssh.py @@ -105,7 +105,7 @@ def establish_connection(self, sleep_time=3, verbose=True, timeout=8, use_keys=F # Strip the initial router prompt time.sleep(sleep_time) - return self.remote_conn.recv(MAX_BUFFER).decode('utf-8') + return self.remote_conn.recv(MAX_BUFFER).decode('utf-8', 'ignore') def config_mode(self, config_command=''): ''' diff --git a/netmiko/hp/hp_comware_ssh.py b/netmiko/hp/hp_comware_ssh.py index 0f751bf06..90be9db6d 100644 --- a/netmiko/hp/hp_comware_ssh.py +++ b/netmiko/hp/hp_comware_ssh.py @@ -70,7 +70,7 @@ def set_base_prompt(self, pri_prompt_terminator='>', alt_prompt_terminator=']', self.remote_conn.sendall("\n") time.sleep(1 * delay_factor) - prompt = self.remote_conn.recv(MAX_BUFFER).decode('utf-8') + prompt = self.remote_conn.recv(MAX_BUFFER).decode('utf-8', 'ignore') prompt = self.normalize_linefeeds(prompt) # If multiple lines in the output take the last line diff --git a/netmiko/huawei/huawei_ssh.py b/netmiko/huawei/huawei_ssh.py index 70b60c1a4..b87a09c69 100644 --- a/netmiko/huawei/huawei_ssh.py +++ b/netmiko/huawei/huawei_ssh.py @@ -70,7 +70,7 @@ def set_base_prompt(self, pri_prompt_terminator='>', alt_prompt_terminator=']', self.remote_conn.sendall("\n") time.sleep(1 * delay_factor) - prompt = self.remote_conn.recv(MAX_BUFFER).decode('utf-8') + prompt = self.remote_conn.recv(MAX_BUFFER).decode('utf-8', 'ignore') prompt = self.normalize_linefeeds(prompt) # If multiple lines in the output take the last line diff --git a/netmiko/juniper/juniper_ssh.py b/netmiko/juniper/juniper_ssh.py index b0fcdf71d..f85102e11 100644 --- a/netmiko/juniper/juniper_ssh.py +++ b/netmiko/juniper/juniper_ssh.py @@ -31,7 +31,7 @@ def enter_cli_mode(self): while True: self.remote_conn.sendall("\n") time.sleep(1) - cur_prompt = self.remote_conn.recv(MAX_BUFFER).decode('utf-8') + cur_prompt = self.remote_conn.recv(MAX_BUFFER).decode('utf-8', 'ignore') if re.search(r'root@.*%', cur_prompt): self.remote_conn.sendall("cli\n") time.sleep(1)