From d6fbb01a292ec507ba7bc39d145e063e68387987 Mon Sep 17 00:00:00 2001 From: Enrico Jorns Date: Thu, 12 Oct 2023 12:48:48 +0200 Subject: [PATCH] driver/sshdriver: support selecting scp mode explicitly 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 -0 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 --- doc/configuration.rst | 2 ++ labgrid/driver/sshdriver.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/doc/configuration.rst b/doc/configuration.rst index 553418d03..5210b14be 100644 --- a/doc/configuration.rst +++ b/doc/configuration.rst @@ -1573,6 +1573,8 @@ Arguments: target. - explicit_sftp_mode (bool, default=False): if set to True, `put()` and `get()` 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()` and `get()` will + explicitly use the SCP protocol for file transfers instead of scp's default protocol UBootDriver ~~~~~~~~~~~ diff --git a/labgrid/driver/sshdriver.py b/labgrid/driver/sshdriver.py index 108b717cc..6508d236a 100644 --- a/labgrid/driver/sshdriver.py +++ b/labgrid/driver/sshdriver.py @@ -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__() @@ -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( @@ -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._scp_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=''): @@ -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( @@ -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(