2024-08-05 22:12:43 +03:00
|
|
|
import cmd
|
|
|
|
import socket
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
|
|
|
class ModuleShell(cmd.Cmd):
|
|
|
|
intro = "Welcome to the Module Shell. Type 'help' for commands."
|
|
|
|
prompt = "(module_shell) "
|
|
|
|
|
|
|
|
def __init__(self, host='localhost', port=9999):
|
|
|
|
super().__init__()
|
|
|
|
self.host = host
|
|
|
|
self.port = port
|
2024-08-11 22:09:40 +03:00
|
|
|
self.session_id = None
|
2024-08-05 22:12:43 +03:00
|
|
|
|
2024-08-11 22:09:40 +03:00
|
|
|
def send_command(self, action, **kwargs):
|
|
|
|
command = {'action': action, **kwargs}
|
|
|
|
if self.session_id:
|
|
|
|
command['session_id'] = self.session_id
|
2024-08-05 22:12:43 +03:00
|
|
|
|
2024-08-06 22:38:33 +03:00
|
|
|
try:
|
2024-08-11 22:09:40 +03:00
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
|
|
s.connect((self.host, self.port))
|
|
|
|
s.sendall(json.dumps(command).encode())
|
|
|
|
data = s.recv(1024).decode()
|
|
|
|
return json.loads(data) if data else None
|
2024-08-06 22:38:33 +03:00
|
|
|
except Exception as e:
|
2024-08-11 22:09:40 +03:00
|
|
|
print(f"Error: {str(e)}")
|
|
|
|
return None
|
2024-08-05 22:12:43 +03:00
|
|
|
|
|
|
|
def do_load(self, arg):
|
|
|
|
"""Load a module: load <module_name>"""
|
2024-08-11 22:09:40 +03:00
|
|
|
response = self.send_command('load', module=arg)
|
|
|
|
if response:
|
2024-08-06 22:38:33 +03:00
|
|
|
print(response.get('message', 'Unknown response'))
|
2024-08-05 22:12:43 +03:00
|
|
|
|
|
|
|
def do_unload(self, arg):
|
|
|
|
"""Unload a module: unload <module_name>"""
|
2024-08-11 22:09:40 +03:00
|
|
|
response = self.send_command('unload', module=arg)
|
|
|
|
if response:
|
2024-08-06 22:38:33 +03:00
|
|
|
print(response.get('message', 'Unknown response'))
|
2024-08-05 22:12:43 +03:00
|
|
|
|
|
|
|
def do_list(self, arg):
|
|
|
|
"""List all loaded modules"""
|
2024-08-11 22:09:40 +03:00
|
|
|
response = self.send_command('list')
|
|
|
|
if response and response.get('success'):
|
|
|
|
modules = response.get('message', [])
|
|
|
|
if modules:
|
|
|
|
print("Loaded modules:")
|
|
|
|
for module in modules:
|
|
|
|
print(f"- {module}")
|
|
|
|
else:
|
|
|
|
print("No modules are currently loaded.")
|
2024-08-05 22:12:43 +03:00
|
|
|
else:
|
2024-08-11 22:09:40 +03:00
|
|
|
print("Failed to retrieve module list.")
|
2024-08-05 22:12:43 +03:00
|
|
|
|
2024-08-11 22:09:40 +03:00
|
|
|
def do_execute(self, arg):
|
|
|
|
"""Execute a command: execute <command> [args]"""
|
|
|
|
parts = arg.split()
|
|
|
|
if not parts:
|
|
|
|
print("Error: No command specified.")
|
2024-08-06 22:38:33 +03:00
|
|
|
return
|
2024-08-11 22:09:40 +03:00
|
|
|
command = parts[0]
|
|
|
|
args = ' '.join(parts[1:])
|
|
|
|
response = self.send_command('execute', command=command, args=args)
|
|
|
|
if response:
|
2024-08-06 22:38:33 +03:00
|
|
|
print(response.get('message', 'Unknown response'))
|
2024-08-05 22:12:43 +03:00
|
|
|
|
|
|
|
def do_upgrade(self, arg):
|
2024-08-11 22:09:40 +03:00
|
|
|
"""Upgrade the core daemon and modules"""
|
|
|
|
response = self.send_command('upgrade')
|
|
|
|
if response:
|
|
|
|
print(response.get('message', 'Unknown response'))
|
2024-08-05 22:12:43 +03:00
|
|
|
|
|
|
|
def do_exit(self, arg):
|
|
|
|
"""Exit the shell"""
|
|
|
|
print("Exiting...")
|
|
|
|
return True
|
|
|
|
|
2024-08-06 22:38:33 +03:00
|
|
|
def do_shutdown(self, arg):
|
|
|
|
"""Shutdown the core daemon"""
|
2024-08-11 22:09:40 +03:00
|
|
|
response = self.send_command('shutdown')
|
|
|
|
if response:
|
2024-08-06 22:38:33 +03:00
|
|
|
print(response.get('message', 'Unknown response'))
|
|
|
|
print("Core daemon is shutting down. Exiting CLI.")
|
2024-08-11 22:09:40 +03:00
|
|
|
return True
|
|
|
|
|
|
|
|
def default(self, line):
|
|
|
|
"""Handle unknown commands by trying to execute them"""
|
|
|
|
self.do_execute(line)
|
2024-08-06 22:38:33 +03:00
|
|
|
|
2024-08-05 22:12:43 +03:00
|
|
|
def main():
|
|
|
|
ModuleShell().cmdloop()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|