Skip to content

Commit

Permalink
ssh: better error reporting on SshSession errors
Browse files Browse the repository at this point in the history
  • Loading branch information
koreno committed Sep 10, 2020
1 parent 99a9e6e commit b600191
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
12 changes: 10 additions & 2 deletions plumbum/commands/processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ class ProcessExecutionError(EnvironmentError):
well as the command line used to create the process (``argv``)
"""

def __init__(self, argv, retcode, stdout, stderr):
def __init__(self, argv, retcode, stdout, stderr, message=None):
Exception.__init__(self, argv, retcode, stdout, stderr)
self.message = message
self.argv = argv
self.retcode = retcode
if six.PY3 and isinstance(stdout, six.bytes):
Expand All @@ -139,11 +140,18 @@ def __init__(self, argv, retcode, stdout, stderr):
self.stderr = stderr

def __str__(self):
# avoid an import cycle
from plumbum.commands.base import shquote_list
stdout = "\n | ".join(str(self.stdout).splitlines())
stderr = "\n | ".join(str(self.stderr).splitlines())
cmd = " ".join(shquote_list(self.argv))
lines = ["Unexpected exit code: ", str(self.retcode)]
lines = []
if self.message:
lines = [
self.message,
"\nReturn code: | ", str(self.retcode)]
else:
lines = ["Unexpected exit code: ", str(self.retcode)]
cmd = "\n | ".join(cmd.splitlines())
lines += ["\nCommand line: | ", cmd]
if stdout:
Expand Down
29 changes: 22 additions & 7 deletions plumbum/machines/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import threading
from plumbum.commands import BaseCommand, run_proc
from plumbum.commands.processes import ProcessExecutionError
from plumbum.lib import six
from plumbum.machines.base import PopenAddons

Expand All @@ -13,7 +14,7 @@ class ShellSessionError(Exception):
pass


class SSHCommsError(EOFError):
class SSHCommsError(ProcessExecutionError, EOFError):
"""Raises when the communication channel can't be created on the
remote host or it times out."""

Expand Down Expand Up @@ -124,16 +125,30 @@ def communicate(self, input=None):

self.proc.poll()
returncode = self.proc.returncode
stdout = six.b("").join(stdout).decode(self.custom_encoding, "ignore")
stderr = six.b("").join(stderr).decode(self.custom_encoding, "ignore")
argv = self.argv.decode(self.custom_encoding, "ignore").split(";")[:1]

if returncode == 5:
raise IncorrectLogin(
"Incorrect username or password provided")
argv, returncode, stdout, stderr,
message="Incorrect username or password provided")
elif returncode == 6:
raise HostPublicKeyUnknown(
"The authenticity of the host can't be established")
msg = "No communication channel detected. Does the remote exist?"
msgerr = "No stderr result detected. Does the remote have Bash as the default shell?"
raise SSHCommsChannel2Error(
msgerr) if name == "2" else SSHCommsError(msg)
argv, returncode, stdout, stderr,
message="The authenticity of the host can't be established")
elif returncode != 0:
raise SSHCommsError(
argv, returncode, stdout, stderr,
message="SSH communication failed")
elif name == "2":
raise SSHCommsChannel2Error(
argv, returncode, stdout, stderr,
message="No stderr result detected. Does the remote have Bash as the default shell?")
else:
raise SSHCommsError(
argv, returncode, stdout, stderr,
message="No communication channel detected. Does the remote exist?")
if not line:
del sources[i]
else:
Expand Down

0 comments on commit b600191

Please sign in to comment.