xref: /linux/tools/testing/selftests/drivers/net/lib/py/remote_ssh.py (revision d53b8e36925256097a08d7cb749198d85cbf9b2b)
1# SPDX-License-Identifier: GPL-2.0
2
3import os
4import string
5import subprocess
6import random
7
8from lib.py import cmd
9
10
11class Remote:
12    def __init__(self, name, dir_path):
13        self.name = name
14        self.dir_path = dir_path
15        self._tmpdir = None
16
17    def __del__(self):
18        if self._tmpdir:
19            cmd("rm -rf " + self._tmpdir, host=self)
20            self._tmpdir = None
21
22    def cmd(self, comm):
23        return subprocess.Popen(["ssh", "-q", self.name, comm],
24                                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
25
26    def _mktmp(self):
27        return ''.join(random.choice(string.ascii_lowercase) for _ in range(8))
28
29    def deploy(self, what):
30        if not self._tmpdir:
31            self._tmpdir = "/tmp/" + self._mktmp()
32            cmd("mkdir " + self._tmpdir, host=self)
33        file_name = self._tmpdir + "/" + self._mktmp() + os.path.basename(what)
34
35        if not os.path.isabs(what):
36            what = os.path.abspath(self.dir_path + "/" + what)
37
38        cmd(f"scp {what} {self.name}:{file_name}")
39        return file_name
40