From b537b33782ca6b75d2f91fe2e3469c143ee504b4 Mon Sep 17 00:00:00 2001 From: Craig Tomkow Date: Thu, 23 Jan 2020 14:27:17 -0800 Subject: [PATCH 1/3] the --set flag now saves the configuration after being applied. Currently working for juniper, cisco, and hp. need to do PDU and paloalto --- device_connector.py | 22 ++++++++++++++++++++-- swITch.py | 1 + 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/device_connector.py b/device_connector.py index 7f55ec3..26d7c73 100644 --- a/device_connector.py +++ b/device_connector.py @@ -131,9 +131,27 @@ def send_command(self, cmd): def send_config_set(self, set_list): if self.device_type == 'juniper_junos': - return self.device_connection.send_config_set(set_list, config_mode_command='configure exclusive') + return self.device_connection.send_config_set(set_list, exit_config_mode=False, config_mode_command='configure exclusive') + elif self.device_type == 'cisco_ios': + return self.device_connection.send_config_set(set_list, exit_config_mode=True) + elif self.device_type == 'hp_procurve': + return self.device_connection.send_config_set(set_list, exit_config_mode=True) + else: + pass + + def save_config_and_exit(self): + + buf = "" + if self.device_type == 'juniper_junos': + buf += self.device_connection.commit(and_quit=True) + elif self.device_type == 'cisco_ios': + buf += self.device_connection.save_config() + elif self.device_type == 'hp_procurve': + buf += self.device_connection.save_config() else: - return self.device_connection.send_config_set(set_list) + pass + buf += self.device_connection.exit_config_mode() + return buf def disconnect(self): diff --git a/swITch.py b/swITch.py index e67c1b2..09ddd03 100755 --- a/swITch.py +++ b/swITch.py @@ -213,6 +213,7 @@ def main(self, auth, commands, debug, enable, ip_list, log_only, port_list, set, log.event('verbose', dev.find_prompt() + cmd + "\n") log.event('log_only', dev.send_config_set(list_of_set_cmds) + "\n") # send command log.event('debug', "DEBUG PROMPT:" + dev.find_prompt() + "\n") + log.event('verbose', dev.save_config_and_exit() + "\n") dev.disconnect() log.event('info', "SSH connection closed to " + dev.ip + "\n") From 9edde8f5ab7f860a3a196e807a30452f49e11233 Mon Sep 17 00:00:00 2001 From: Craig Tomkow Date: Thu, 23 Jan 2020 15:50:33 -0800 Subject: [PATCH 2/3] added sentry_pdu support for the --set flag. note: the pdu doesn't explicitely needing saved, however. --- device_connector.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/device_connector.py b/device_connector.py index 26d7c73..9aecc20 100644 --- a/device_connector.py +++ b/device_connector.py @@ -136,20 +136,28 @@ def send_config_set(self, set_list): return self.device_connection.send_config_set(set_list, exit_config_mode=True) elif self.device_type == 'hp_procurve': return self.device_connection.send_config_set(set_list, exit_config_mode=True) + elif self.device_type == 'sentry_pdu': + return self.device_connection.send_config_set(set_list, exit_config_mode=False) else: pass def save_config_and_exit(self): buf = "" - if self.device_type == 'juniper_junos': - buf += self.device_connection.commit(and_quit=True) - elif self.device_type == 'cisco_ios': - buf += self.device_connection.save_config() - elif self.device_type == 'hp_procurve': - buf += self.device_connection.save_config() - else: - pass + try: + if self.device_type == 'juniper_junos': + buf += self.device_connection.commit(and_quit=True) + elif self.device_type == 'cisco_ios': + buf += self.device_connection.save_config() + elif self.device_type == 'hp_procurve': + buf += self.device_connection.save_config() + elif self.device_type == 'sentry_pdu': + buf += self.device_connection.save_config() + else: + pass + except NotImplementedError: # e.g. sentry_pdu uses netmiko's accedian device and it doesn't impl save_config + return buf + buf += self.device_connection.exit_config_mode() return buf From 44b06b247b950f3e718ad370c4bffdc796c7168c Mon Sep 17 00:00:00 2001 From: Craig Tomkow Date: Fri, 24 Jan 2020 09:54:22 -0800 Subject: [PATCH 3/3] added committing of config for the last supported device, paloalto --- device_connector.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/device_connector.py b/device_connector.py index 9aecc20..4437275 100644 --- a/device_connector.py +++ b/device_connector.py @@ -138,25 +138,33 @@ def send_config_set(self, set_list): return self.device_connection.send_config_set(set_list, exit_config_mode=True) elif self.device_type == 'sentry_pdu': return self.device_connection.send_config_set(set_list, exit_config_mode=False) + elif self.device_type == 'paloalto_panos': + try: + return self.device_connection.send_config_set(set_list, exit_config_mode=False) + except OSError: + return "Did not find expected pattern. Likely nothing to commit!" else: pass def save_config_and_exit(self): buf = "" - try: - if self.device_type == 'juniper_junos': - buf += self.device_connection.commit(and_quit=True) - elif self.device_type == 'cisco_ios': - buf += self.device_connection.save_config() - elif self.device_type == 'hp_procurve': - buf += self.device_connection.save_config() - elif self.device_type == 'sentry_pdu': + + if self.device_type == 'juniper_junos': + buf += self.device_connection.commit(and_quit=True) + elif self.device_type == 'cisco_ios': + buf += self.device_connection.save_config() + elif self.device_type == 'hp_procurve': + buf += self.device_connection.save_config() + elif self.device_type == 'sentry_pdu': + try: # e.g. sentry_pdu uses netmiko's accedian device - it doesn't impl save_config as config is auto-saved buf += self.device_connection.save_config() - else: - pass - except NotImplementedError: # e.g. sentry_pdu uses netmiko's accedian device and it doesn't impl save_config - return buf + except NotImplementedError: + return buf + elif self.device_type == 'paloalto_panos': + buf += self.device_connection.commit() + else: + pass buf += self.device_connection.exit_config_mode() return buf