mirror of
https://github.com/thingsboard/thingsboard-gateway
synced 2025-10-26 22:31:42 +08:00
Remote shell activation added
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
thingsboard:
|
||||
host: demo.thingsboard.io
|
||||
port: 1883
|
||||
remoteShell: false
|
||||
remoteConfiguration: false
|
||||
security:
|
||||
accessToken: PUT_YOUR_GW_ACCESS_TOKEN_HERE
|
||||
|
||||
@@ -116,7 +116,10 @@ class TBGatewayService:
|
||||
"update": self.__rpc_update,
|
||||
"version": self.__rpc_version,
|
||||
}
|
||||
self.__remote_shell = RemoteShell(platform=self.__updater.get_platform(), release=self.__updater.get_release())
|
||||
self.__remote_shell = None
|
||||
if config["thingsboard"].get("remoteShell"):
|
||||
log.warning("Remote shell is enabled. Please be carefully with this feature.")
|
||||
self.__remote_shell = RemoteShell(platform=self.__updater.get_platform(), release=self.__updater.get_release())
|
||||
self.__rpc_remote_shell_command_in_progress = None
|
||||
self.__sheduled_rpc_calls = []
|
||||
self.__rpc_sheduled_methods_functions = {
|
||||
@@ -497,13 +500,18 @@ class TBGatewayService:
|
||||
arguments.update({"timeout": content["timeout"]})
|
||||
method_to_call = content["method"].replace("gateway_", "")
|
||||
result = None
|
||||
method_function = self.__remote_shell.shell_commands.get(method_to_call, self.__gateway_rpc_methods.get(method_to_call))
|
||||
if self.__remote_shell is not None:
|
||||
method_function = self.__remote_shell.shell_commands.get(method_to_call, self.__gateway_rpc_methods.get(method_to_call))
|
||||
else:
|
||||
log.info("Remote shell is disabled.")
|
||||
method_function = self.__gateway_rpc_methods.get(method_to_call)
|
||||
if method_function is None and method_to_call in self.__rpc_sheduled_methods_functions:
|
||||
seconds_to_restart = arguments*1000 if arguments and arguments != '{}' else 0
|
||||
self.__sheduled_rpc_calls.append([time() * 1000 + seconds_to_restart, self.__rpc_sheduled_methods_functions[method_to_call]])
|
||||
log.info("Gateway %s scheduled in %i seconds", method_to_call, seconds_to_restart/1000)
|
||||
result = {"success": True}
|
||||
elif method_function is None:
|
||||
log.error("RPC method %s - Not found", content["method"])
|
||||
return {"error": "Method not found", "code": 404}
|
||||
elif isinstance(arguments, list):
|
||||
result = method_function(*arguments)
|
||||
|
||||
@@ -82,10 +82,10 @@ class RemoteShell:
|
||||
log.debug("Process is run")
|
||||
stdout_value = b"" if e.stdout is None else e.stdout.replace(self.__previous_stdout, b"")
|
||||
stderr_value = b"" if e.stderr is None else e.stderr.replace(self.__previous_stderr, b"")
|
||||
stdout_value = stdout_value[:-1] if len(stdout_value)>0 and stdout_value[-1] == b"\n" else stdout_value
|
||||
stderr_value = stderr_value[:-1] if len(stderr_value)>0 and stderr_value[-1] == b"\n" else stderr_value
|
||||
self.__previous_stderr = self.__previous_stderr + stderr_value
|
||||
self.__previous_stdout = self.__previous_stdout + stdout_value
|
||||
stdout_value = stdout_value[:-1] if len(stdout_value) and stdout_value[-1] == b"\n" else stdout_value
|
||||
stderr_value = stderr_value[:-1] if len(stderr_value) and stderr_value[-1] == b"\n" else stderr_value
|
||||
self.__previous_stderr = self.__previous_stderr + b"" if stderr_value is None else stderr_value
|
||||
self.__previous_stdout = self.__previous_stdout + b"" if stdout_value is None else stdout_value
|
||||
str_stdout = str(stdout_value, "UTF-8") if isinstance(stdout_value, bytes) else stdout_value
|
||||
str_stderr = str(stderr_value, "UTF-8") if isinstance(stderr_value, bytes) else stderr_value
|
||||
result.update({"data": [{"stdout": str_stdout,
|
||||
|
||||
Reference in New Issue
Block a user