Skip to content

Commit 3810d95

Browse files
committed
Allow to specify ssh config file to use for control master ssh connection
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.
1 parent 4c1d7cb commit 3810d95

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/lbpytest/controlmaster.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class SSH:
1818
exactly one connection and its corresponding ControlMaster socket.
1919
"""
2020

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

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

59-
subprocess.run(['ssh', '-S', self.sockpath, '-l', self.user,
64+
user_args = []
65+
if self.user:
66+
user_args += ['-l', self.user]
67+
config_args = []
68+
if self.ssh_config:
69+
config_args += ['-F', self.ssh_config]
70+
71+
subprocess.run(['ssh', '-S', self.sockpath] + user_args + config_args + [
6072
'-f', # Requests ssh to go to background just before command execution.
6173
'-N', # Do not execute a remote command.
6274
'-M', # Places the ssh client into "master" mode for connection sharing.

0 commit comments

Comments
 (0)