Skip to content

Commit

Permalink
Allow to specify ssh config file to use for control master ssh connec…
Browse files Browse the repository at this point in the history
…tion

For example, virter creates a file called /etc/ssh/ssh_config.virter
which contains user names for the various vms (root for Linux,
Administrator for Windows). Also when None is specified for the
user then no -l option is added to the ssh command line.
  • Loading branch information
johannesthoma committed Feb 6, 2025
1 parent 4c1d7cb commit 3810d95
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/lbpytest/controlmaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SSH:
exactly one connection and its corresponding ControlMaster socket.
"""

def __init__(self, host, user='root', basedir='/tmp', timeout=None, connection_timeout=None):
def __init__(self, host, user='root', basedir='/tmp', timeout=None, connection_timeout=None, ssh_config=None):
"""
The constructor immediately connects to a remote host and stores the
ControlMaster socket in the local file system.
Expand All @@ -31,6 +31,10 @@ def __init__(self, host, user='root', basedir='/tmp', timeout=None, connection_t
:param timeout: the default timeout for subsequent calls to ``run()``
:param connection_timeout: the timeout for the connection attempt;
defaults to ``timeout``
:param ssh_config: path to the ssh config file.
defaults to None (do not pass a -F argument)
useful value is /etc/ssh/ssh_config.virter for a virter generated
config.
:raises subprocess.CalledProcessError: When the ssh connection cannot
be established. The exception contains the captured stdout and
stderr from the ssh command.
Expand All @@ -39,6 +43,7 @@ def __init__(self, host, user='root', basedir='/tmp', timeout=None, connection_t
self.host = host
self.sockpath = os.path.join(basedir, 'controlmaster-'+self.host+'-'+uuid.uuid4().hex)
self.timeout = timeout
self.ssh_config = ssh_config

# With the combination of flags below, older versions of ssh fail to
# close stderr. This was fixed in release 8.5 by
Expand All @@ -56,7 +61,14 @@ def __init__(self, host, user='root', basedir='/tmp', timeout=None, connection_t
if major < 8 or (major == 8 and minor < 5):
raise RuntimeError("unsupported ssh version: '{}'".format(output))

subprocess.run(['ssh', '-S', self.sockpath, '-l', self.user,
user_args = []
if self.user:
user_args += ['-l', self.user]
config_args = []
if self.ssh_config:
config_args += ['-F', self.ssh_config]

subprocess.run(['ssh', '-S', self.sockpath] + user_args + config_args + [
'-f', # Requests ssh to go to background just before command execution.
'-N', # Do not execute a remote command.
'-M', # Places the ssh client into "master" mode for connection sharing.
Expand Down

0 comments on commit 3810d95

Please sign in to comment.