From 94ea2fdff7b100d10d777481faad0ee7d95455e9 Mon Sep 17 00:00:00 2001 From: Alex X Date: Sat, 7 Dec 2024 10:15:02 +0300 Subject: [PATCH] Add support multiple commands call --- custom_components/ssh_command/__init__.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/custom_components/ssh_command/__init__.py b/custom_components/ssh_command/__init__.py index c94408a..39f75f6 100644 --- a/custom_components/ssh_command/__init__.py +++ b/custom_components/ssh_command/__init__.py @@ -35,7 +35,9 @@ def exec_command(call: ServiceCall) -> dict: kwargs["password"] = kwargs.pop("pass", None) kwargs["key_filename"] = kwargs.pop("private_key", None) - command = kwargs.pop("command") + commands = kwargs.pop("command") + if isinstance(commands, str): + commands = [commands] client = SSHClient() client.set_missing_host_key_policy(AutoAddPolicy()) @@ -43,10 +45,12 @@ def exec_command(call: ServiceCall) -> dict: try: client.connect(**kwargs) - _, stdout, stderr = client.exec_command( - command, timeout=kwargs.get("timeout") - ) + for command in commands: + _, stdout, stderr = client.exec_command( + command, timeout=kwargs.get("timeout") + ) + # noinspection PyUnboundLocalVariable return { "stdout": stdout.read().decode("utf-8"), "stderr": stderr.read().decode("utf-8"),