Skip to content

Commit

Permalink
Merge pull request #30 from RicterZ/master
Browse files Browse the repository at this point in the history
add timeout option for ssh connection and command execution
  • Loading branch information
AlexxIT authored Oct 15, 2024
2 parents edf3cd1 + 072025c commit e6b50e9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ script:
user: pi
pass: raspberry
command: ls -la
timeout: 5
```
If you want connect with ssh key, use this:
Expand All @@ -47,6 +48,7 @@ script:
user: pi
private_key: /config/ssh/id_rsa
command: ls -la
timeout: 5
```
If you want use secrets or change default values, add them to `configuration.yaml`:
Expand All @@ -57,4 +59,5 @@ ssh_command:
port: 22
user: pi
pass: !secret ssh_parssword
timeout: 5
```
13 changes: 10 additions & 3 deletions custom_components/ssh_command/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
vol.Optional("port", default=22): cv.port,
vol.Optional("user", default="pi"): cv.string,
vol.Optional("pass", default="raspberry"): cv.string,
vol.Optional("timeout", default=5): cv.positive_int,
},
extra=vol.PREVENT_EXTRA,
)
Expand All @@ -30,6 +31,7 @@ def exec_command(call: ServiceCall):
port = call.data.get("port", default["port"])
username = call.data.get("user", default["user"])
password = call.data.get("pass", default["pass"])
timeout = call.data.get("timeout", default["timeout"])
command = call.data["command"]

client = SSHClient()
Expand All @@ -38,15 +40,20 @@ def exec_command(call: ServiceCall):
try:
if private_key := call.data.get("private_key"):
key = RSAKey.from_private_key_file(private_key)
client.connect(host, port, username, pkey=key)
client.connect(host, port, username, pkey=key, timeout=timeout)
else:
# Use password for authentication if SSH key is not provided
client.connect(host, port, username, password)
client.connect(host, port, username, password, timeout=timeout)
except Exception as e:
_LOGGER.error(f"Failed to connect: {repr(e)}")
return {"error": repr(e)}

_, stdout, stderr = client.exec_command(command)
try:
_, stdout, stderr = client.exec_command(command, timeout=timeout)
except TimeoutError as e:
_LOGGER.error(f"Command execution timeout")
return {"error": repr(e)}

response = {
"command": command,
"stdout": stdout.read().decode("utf-8"),
Expand Down
7 changes: 7 additions & 0 deletions custom_components/ssh_command/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ exec_command:
example: /config/ssh/id_rsa
selector:
text:
timeout:
example: 30
default: 5
selector:
number:
min: 1
max: 60
6 changes: 5 additions & 1 deletion custom_components/ssh_command/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@
"private_key": {
"name": "Private key",
"description": ""
},
"timeout": {
"name": "Connection timeout",
"description": ""
}
}
}
}
}
}

0 comments on commit e6b50e9

Please sign in to comment.