Skip to content

Commit

Permalink
automatically try to open session when running command on an inactive…
Browse files Browse the repository at this point in the history
… session (#12)
  • Loading branch information
t-cas committed Jul 21, 2017
1 parent 1c0a07d commit 4ac1441
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
For next release
----------------
- automatically try to open session when running command on an inactive session

1.1.0 (07/20/2017)
------------------
- Each ssh session can be used as a jump server to access multiple remote sessions in parallel. Only 1 remote
Expand Down
9 changes: 4 additions & 5 deletions jumpssh/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,13 @@ def run_cmd(
# need to run full command with shell to support shell builtins commands (source, ...)
my_cmd = 'sudo su - %s -c "%s"' % (user, cmd.replace('"', '\\"'))

# check session is still active before running a command, else try to open it
if not self.is_active():
self.open()

if not silent:
logger.debug("Running command '%s' on '%s' as %s..." % (cmd, self.host, user))

# check session is still active before running a command
if not self.ssh_transport.is_active():
raise exception.SSHException("Unable to run command '%s', SSH session to '%s' is closed"
% (cmd, self.host))

channel = self.ssh_transport.open_session()

# raise error rather than blocking the call
Expand Down
5 changes: 2 additions & 3 deletions tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ def test_active_close_session(docker_env):
# closing a closed session does nothing
gateway_session.close()

# running command on an inactive session raise a SSHException
with pytest.raises(exception.SSHException):
gateway_session.run_cmd('ls')
# running command on an inactive session will automatically open the session
assert gateway_session.run_cmd('ls').exit_code == 0


def test_active_close_session_with_context_manager(docker_env):
Expand Down

0 comments on commit 4ac1441

Please sign in to comment.