38 lines
1.5 KiB
Python
38 lines
1.5 KiB
Python
from app.models.device import Device
|
|
from app.models.object import Object
|
|
from app.plugins.base import ActionResult, BaseAction, Emitter
|
|
from app.services import ssh as ssh_service
|
|
|
|
|
|
class SSHCommandAction(BaseAction):
|
|
name = "ssh_command"
|
|
display_name = "Run SSH Command"
|
|
compatible_categories = ["raspberry", "server", "embedded", "other"]
|
|
params_schema = [
|
|
{"name": "command", "type": "string", "label": "Command", "required": True},
|
|
]
|
|
|
|
async def execute(self, device: Device, obj: Object, params: dict, emit: Emitter) -> ActionResult:
|
|
command = params.get("command", "").strip()
|
|
if not command:
|
|
return ActionResult(success=False, error="No command specified")
|
|
|
|
await emit(f"[{device.ip}] $ {command}", "info")
|
|
result = await ssh_service.run_command(device, obj, command)
|
|
|
|
if result.error:
|
|
await emit(f"[{device.ip}] Error: {result.error}", "error")
|
|
else:
|
|
if result.stdout:
|
|
await emit(f"[{device.ip}] {result.stdout}", "info")
|
|
if result.stderr:
|
|
await emit(f"[{device.ip}] stderr: {result.stderr}", "warn")
|
|
await emit(f"[{device.ip}] exit_code={result.exit_code} ({result.duration_ms}ms)", "info")
|
|
|
|
return ActionResult(
|
|
success=result.success,
|
|
stdout=result.stdout,
|
|
stderr=result.stderr,
|
|
exit_code=result.exit_code,
|
|
error=result.error,
|
|
)
|