Skip to content

Commit

Permalink
Added exception handlers to SshConnection (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
chughes741 authored Aug 4, 2023
1 parent ceed7f9 commit 1a2f3eb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion openct/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
if device.is_available():
device.fetch_backup()
else:
logging.error("Could not connect to device")
logging.error("Could not connect to device %s", host)
22 changes: 15 additions & 7 deletions openct/connections/connections.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Module for connection protocols"""

from typing import Protocol
import logging

from fabric import Connection as FabricConnection
from paramiko.ssh_exception import SSHException
from paramiko.ssh_exception import SSHException, NoValidConnectionsError
from invoke import UnexpectedExit


class DeviceConnection(Protocol):
Expand Down Expand Up @@ -43,7 +45,8 @@ def test_connection(self) -> bool:
try:
connection.open()
return True
except (TimeoutError, SSHException):
except (TimeoutError, SSHException, NoValidConnectionsError):
logging.error("Could not connect to device %s", self.ip_address)
return False

def fetch_backup(self) -> None:
Expand All @@ -53,8 +56,13 @@ def fetch_backup(self) -> None:
connect_timeout=self.connection_timeout,
connect_kwargs={"key_filename": self.key_file},
) as connection:
connection.run("/export file=backup", hide=True, warn=False)
connection.get(
"backup.rsc", f"{self.backup_dir}/backup_{self.ip_address}.rsc"
)
connection.run("file/remove backup.rsc", hide=True, warn=False)
try:
connection.run("/export file=backup", hide=True, warn=False)
connection.get(
"backup.rsc", f"{self.backup_dir}/backup_{self.ip_address}.rsc"
)
connection.run("file/remove backup.rsc", hide=True, warn=False)
except UnexpectedExit:
logging.error(
"Error while fetching backup from device %s", self.ip_address
)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "openct"
version = "0.1.2"
version = "0.1.3"
description = "Configuration backup and analysis tools for devices running pfSense and RouterOS"
authors = ["Weehooey <[email protected]>"]
license = "GNU GPL v3.0"
Expand Down

0 comments on commit 1a2f3eb

Please sign in to comment.