From 6ab64b0ac06ca41388acb30a467acd23a0e6e691 Mon Sep 17 00:00:00 2001 From: Vladimir Lila Date: Thu, 11 Apr 2024 00:50:01 +0500 Subject: [PATCH 1/2] Add optional ssh key path --- custom_components/ssh_command/__init__.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/custom_components/ssh_command/__init__.py b/custom_components/ssh_command/__init__.py index dbbd038..eefa961 100644 --- a/custom_components/ssh_command/__init__.py +++ b/custom_components/ssh_command/__init__.py @@ -1,7 +1,7 @@ import logging from homeassistant.core import ServiceCall -from paramiko import SSHClient, AutoAddPolicy +from paramiko import AutoAddPolicy, RSAKey, SSHClient _LOGGER = logging.getLogger(__name__) @@ -15,9 +15,22 @@ async def exec_command(call: ServiceCall): username = call.data.get('user', 'pi') password = call.data.get('pass', 'raspberry') command = call.data.get('command') + ssh_key_path = call.data.get('ssh_key_path') client = SSHClient() client.set_missing_host_key_policy(AutoAddPolicy()) + + if ssh_key_path: + try: + key = RSAKey(filename=ssh_key_path) + client.connect(host, port, username, pkey=key) + except Exception as e: + _LOGGER.error(f"Failed to connect using SSH key: {e}") + return + else: + # Use password for authentication if SSH key is not provided + client.connect(host, port, username, password) + client.connect(host, port, username, password) stdin, stdout, stderr = client.exec_command(command) data = stdout.read() From d93dc6e4d6006b970a38802cd9ffec6d1bc88c3f Mon Sep 17 00:00:00 2001 From: Vladimir Lila Date: Thu, 11 Apr 2024 01:32:18 +0500 Subject: [PATCH 2/2] Add connection with ssh key optional --- README.md | 15 +++++++++++++++ custom_components/ssh_command/__init__.py | 9 ++++----- custom_components/ssh_command/services.yaml | 5 +++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c8f1538..90cd1bd 100644 --- a/README.md +++ b/README.md @@ -41,4 +41,19 @@ script: user: pi pass: raspberry command: ls -la +``` + +if you want connect with ssh key, use this + +```yaml +script: + run_on_host: + alias: Run shell command on host + sequence: + - service: ssh_command.exec_command + data: + host: 192.168.1.123 + user: pi + ssh_private_key_path: /config/ssh/id_rsa + command: ls -la ``` \ No newline at end of file diff --git a/custom_components/ssh_command/__init__.py b/custom_components/ssh_command/__init__.py index eefa961..8af7467 100644 --- a/custom_components/ssh_command/__init__.py +++ b/custom_components/ssh_command/__init__.py @@ -15,14 +15,14 @@ async def exec_command(call: ServiceCall): username = call.data.get('user', 'pi') password = call.data.get('pass', 'raspberry') command = call.data.get('command') - ssh_key_path = call.data.get('ssh_key_path') + ssh_private_key_path = call.data.get('ssh_private_key_path') client = SSHClient() client.set_missing_host_key_policy(AutoAddPolicy()) - if ssh_key_path: + if ssh_private_key_path: try: - key = RSAKey(filename=ssh_key_path) + key = RSAKey.from_private_key_file(ssh_private_key_path) client.connect(host, port, username, pkey=key) except Exception as e: _LOGGER.error(f"Failed to connect using SSH key: {e}") @@ -31,7 +31,6 @@ async def exec_command(call: ServiceCall): # Use password for authentication if SSH key is not provided client.connect(host, port, username, password) - client.connect(host, port, username, password) stdin, stdout, stderr = client.exec_command(command) data = stdout.read() stderr.read() @@ -45,4 +44,4 @@ async def exec_command(call: ServiceCall): async def async_setup_entry(hass, entry): - return True + return True \ No newline at end of file diff --git a/custom_components/ssh_command/services.yaml b/custom_components/ssh_command/services.yaml index f49e438..134a280 100644 --- a/custom_components/ssh_command/services.yaml +++ b/custom_components/ssh_command/services.yaml @@ -33,3 +33,8 @@ exec_command: required: true selector: text: + + ssh_private_key_path: + example: /config/ssh/id_rsa + selector: + text: