tests: Allow user name to be skipped in ssh connection with remotehost

Allow to skip user name when we run ssh user@hostname.

We can specify user name in .ssh/config and simplify using network name
spaces mapped per user.

An example .ssh/config:

 Host localhost
    HostName localhost
    User root

 Host test1
    HostName testpc
    User wlan1

Signed-off-by: Janusz Dziedzic <janusz.dziedzic@gmail.com>
This commit is contained in:
Janusz Dziedzic 2024-05-30 21:19:03 +02:00 committed by Jouni Malinen
parent 9ff4fd30ff
commit 205dbb0fef

View file

@ -83,7 +83,10 @@ class Host():
if self.host is None: if self.host is None:
return self.local_execute(command) return self.local_execute(command)
if self.user:
cmd = ["ssh", self.user + "@" + self.host, ' '.join(command)] cmd = ["ssh", self.user + "@" + self.host, ' '.join(command)]
else:
cmd = ["ssh", self.host, ' '.join(command)]
_cmd = self.name + " execute: " + ' '.join(cmd) _cmd = self.name + " execute: " + ' '.join(cmd)
logger.debug(_cmd) logger.debug(_cmd)
err = tempfile.TemporaryFile() err = tempfile.TemporaryFile()
@ -114,7 +117,10 @@ class Host():
if self.host is None: if self.host is None:
cmd = _command cmd = _command
else: else:
if self.user:
cmd = ["ssh", self.user + "@" + self.host, ' '.join(_command)] cmd = ["ssh", self.user + "@" + self.host, ' '.join(_command)]
else:
cmd = ["ssh", self.host, ' '.join(_command)]
_cmd = self.name + " thread_run: " + ' '.join(cmd) _cmd = self.name + " thread_run: " + ' '.join(cmd)
logger.debug(_cmd) logger.debug(_cmd)
t = threading.Thread(target=execute_thread, name=filename, args=(cmd, res)) t = threading.Thread(target=execute_thread, name=filename, args=(cmd, res))
@ -174,7 +180,10 @@ class Host():
_command = [filename] + command _command = [filename] + command
if self.host: if self.host:
if self.user:
cmd = ["ssh", self.user + "@" + self.host, ' '.join(_command)] cmd = ["ssh", self.user + "@" + self.host, ' '.join(_command)]
else:
cmd = ["ssh", self.host, ' '.join(_command)]
else: else:
cmd = _command cmd = _command
@ -261,12 +270,18 @@ class Host():
def get_logs(self, local_log_dir=None): def get_logs(self, local_log_dir=None):
for log in self.logs: for log in self.logs:
if local_log_dir: if local_log_dir:
if self.user:
self.local_execute(["scp", self.user + "@[" + self.host + "]:" + log, local_log_dir]) self.local_execute(["scp", self.user + "@[" + self.host + "]:" + log, local_log_dir])
else:
self.local_execute(["scp", "[" + self.host + "]:" + log, local_log_dir])
self.execute(["rm", log]) self.execute(["rm", log])
del self.logs[:] del self.logs[:]
def send_file(self, src, dst): def send_file(self, src, dst):
if self.host is None: if self.host is None:
return return
if self.user:
self.local_execute(["scp", src, self.local_execute(["scp", src,
self.user + "@[" + self.host + "]:" + dst]) self.user + "@[" + self.host + "]:" + dst])
else:
self.local_execute(["scp", src, "[" + self.host + "]:" + dst])