Advertisement
DOGGYWOOF

client

Aug 28th, 2024 (edited)
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. -- Function to handle incoming commands
  2. function handleCommand(command)
  3. if command == "/disk/ACPI/shutdown" then
  4. print("[INFO] Shutdown command received.")
  5. os.shutdown()
  6. elseif command == "/disk/ACPI/reboot" then
  7. print("[INFO] Reboot command received.")
  8. os.reboot()
  9. elseif command == "/disk/ACPI/logoff" then
  10. print("[INFO] Logoff command received.")
  11. -- Implement logoff behavior if needed, or shutdown/reboot as an alternative
  12. print("Logoff command is not supported. Please use shutdown or reboot.")
  13. else
  14. print("[ERROR] Unknown command: " .. command)
  15. end
  16. end
  17.  
  18. -- Function to start the client and listen for commands
  19. function startClient()
  20. -- Automatically detect a modem and open rednet
  21. local modemSide
  22. for _, side in ipairs({"left", "right", "top", "bottom", "front", "back"}) do
  23. if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  24. modemSide = side
  25. break
  26. end
  27. end
  28.  
  29. if modemSide then
  30. rednet.open(modemSide)
  31. print("[INFO] Modem found and opened on side: " .. modemSide)
  32.  
  33. -- Set the ID for this computer (this ID should match the ID used in the server script)
  34. local computerID = os.getComputerID()
  35.  
  36. print("[INFO] Listening for commands on ID: " .. computerID)
  37. while true do
  38. local senderID, command = rednet.receive("control")
  39. if senderID then
  40. print("[INFO] Command received from ID: " .. senderID)
  41. handleCommand(command)
  42. end
  43. end
  44.  
  45. -- Close the modem (though this is never reached in this loop)
  46. rednet.close(modemSide)
  47. else
  48. print("[ERROR] No modem found. Please connect a modem to run the client.")
  49. end
  50. end
  51.  
  52. -- Run the client program
  53. startClient()
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement