Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- client.lua
- -- Function to find and open the modem
- local function openModem()
- local sides = {"left", "right", "top", "bottom", "front", "back"}
- for _, side in ipairs(sides) do
- if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
- rednet.open(side)
- return true
- end
- end
- return false
- end
- -- Open the modem
- if not openModem() then
- print("No modem found!")
- return
- end
- local paired = false
- -- Function to pair with the host
- local function pairWithHost(pairingKey)
- rednet.broadcast(pairingKey, "pairing")
- local senderId, response, protocol = rednet.receive("pairing_response", 30)
- if protocol == "pairing_response" and response == "success" then
- paired = true
- print("Paired with host.")
- else
- print("Pairing failed or timed out.")
- end
- end
- -- Main loop
- while true do
- if not paired then
- print("Enter pairing key (4-digit hex): ")
- local pairingKey = read()
- pairWithHost(pairingKey)
- else
- local senderId, command, protocol = rednet.receive("command")
- if protocol == "command" then
- local handle = io.popen(command)
- local result = handle:read("*a")
- handle:close()
- rednet.send(senderId, result, "response")
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment