Advertisement
gur111

remote_commands_cli

Jun 13th, 2025
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.24 KB | None | 0 0
  1. -- Function to open a modem on any side
  2. local function openModem()
  3.   local sides = {"left", "right", "top", "bottom", "front", "back"}
  4.   for _, side in ipairs(sides) do
  5.     if peripheral.getType(side) == "modem" then
  6.       rednet.open(side)
  7.       return true
  8.     end
  9.   end
  10.   return false
  11. end
  12.  
  13. -- CLI function to send commands to the turtle
  14. local function turtleCLI(turtleId)
  15.   if not openModem() then
  16.     print("No modem found. Please attach a modem to the computer.")
  17.     return
  18.   end
  19.  
  20.   print("Connected to turtle " .. turtleId .. ". Type 'exit' to quit.")
  21.  
  22.   while true do
  23.     io.write("> ")
  24.     local command = read()
  25.  
  26.     if command == "exit" then
  27.       break
  28.     end
  29.  
  30.     rednet.send(turtleId, command)
  31.     local senderId, response = rednet.receive(5)  -- Wait for up to 5 seconds for a response
  32.  
  33.     if senderId == turtleId then
  34.       print("Response: " .. response)
  35.     else
  36.       print("No response from turtle " .. turtleId)
  37.     end
  38.   end
  39.  
  40.   rednet.close()
  41. end
  42.  
  43. -- Main function to start the CLI
  44. local args = { ... }
  45. if #args < 1 then
  46.   print("Usage: remote <turtle-id>")
  47. else
  48.   local turtleId = tonumber(args[1])
  49.   if turtleId then
  50.     turtleCLI(turtleId)
  51.   else
  52.     print("Invalid turtle ID.")
  53.   end
  54. end
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement