Skip to content

Commit

Permalink
Add support multiple commands call
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Dec 7, 2024
1 parent 07c2a41 commit 94ea2fd
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions custom_components/ssh_command/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,22 @@ 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())

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"),
Expand Down

0 comments on commit 94ea2fd

Please sign in to comment.