Skip to content

Commit

Permalink
driver/sshdriver: support selecting scp mode explicitly
Browse files Browse the repository at this point in the history
OpenSSH >= 9.0 defaults to the SFTP protocol.
However, some older targets may only have scp installed.
OpenSSH allows to explicitly select scp by providing the -O flag.

As already done for the transition of SFTP, add a dedicated SSHDriver
attribute named 'explicit_scp_mode' which defaults to 'False'.

Signed-off-by: Enrico Jorns <[email protected]>
  • Loading branch information
ejoerns authored and Bastian-Krause committed Oct 13, 2023
1 parent 2c062e3 commit fbd0c8b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,8 @@ Arguments:
target.
- explicit_sftp_mode (bool, default=False): if set to True, `put()`, `get()`, and `scp()` will
explicitly use the SFTP protocol for file transfers instead of scp's default protocol
- explicit_scp_mode (bool, default=False): if set to True, `put()`, `get()`, and `scp()` will
explicitly use the SCP protocol for file transfers instead of scp's default protocol

UBootDriver
~~~~~~~~~~~
Expand Down
15 changes: 15 additions & 0 deletions labgrid/driver/sshdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class SSHDriver(CommandMixin, Driver, CommandProtocol, FileTransferProtocol):
stderr_merge = attr.ib(default=False, validator=attr.validators.instance_of(bool))
connection_timeout = attr.ib(default=float(get_ssh_connect_timeout()), validator=attr.validators.instance_of(float))
explicit_sftp_mode = attr.ib(default=False, validator=attr.validators.instance_of(bool))
explicit_scp_mode = attr.ib(default=False, validator=attr.validators.instance_of(bool))

def __attrs_post_init__(self):
super().__attrs_post_init__()
Expand Down Expand Up @@ -361,6 +362,8 @@ def scp(self, *, src, dst):

if self.explicit_sftp_mode and self._scp_supports_explicit_sftp_mode():
complete_cmd.insert(1, "-s")
if self.explicit_scp_mode and self._scp_supports_explicit_scp_mode():
complete_cmd.insert(1, "-O")

self.logger.info("Running command: %s", complete_cmd)
sub = subprocess.Popen(
Expand Down Expand Up @@ -450,6 +453,14 @@ def _scp_supports_explicit_sftp_mode(self):
return False
raise Exception(f"OpenSSH version {major}.{minor} does not support explicit SFTP mode")

def _scp_supports_explicit_scp_mode(self):
major, minor = self._ssh_version

# OpenSSH >= 9.0 default to the SFTP protocol
if major >= 9:
return True
raise Exception(f"OpenSSH version {major}.{minor} does not support explicit SCP mode")

@Driver.check_active
@step(args=['filename', 'remotepath'])
def put(self, filename, remotepath=''):
Expand All @@ -464,6 +475,8 @@ def put(self, filename, remotepath=''):

if self.explicit_sftp_mode and self._scp_supports_explicit_sftp_mode():
transfer_cmd.insert(1, "-s")
if self.explicit_scp_mode and self._scp_supports_explicit_scp_mode():
transfer_cmd.insert(1, "-O")

try:
sub = subprocess.call(
Expand Down Expand Up @@ -492,6 +505,8 @@ def get(self, filename, destination="."):

if self.explicit_sftp_mode and self._scp_supports_explicit_sftp_mode():
transfer_cmd.insert(1, "-s")
if self.explicit_scp_mode and self._scp_supports_explicit_scp_mode():
transfer_cmd.insert(1, "-O")

try:
sub = subprocess.call(
Expand Down

0 comments on commit fbd0c8b

Please sign in to comment.