Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Remote Control
- -- When installed on a computer, that computer can have files transfered to/from it and commands executed.
- -- Note that after this program is installed, keyboard input can only be given after a certain command is send to the computer.
- -- This program is generally useful as a "boot-loader" kind of program, something to allow the updating and execution of a main program without the need of internet cards or monitor/keyboards.
- local component = require("component")
- local event = require("event")
- local m = component.modem -- get primary modem component
- local fs = require("filesystem")
- local shell = require("shell")
- local process = require("process")
- local PORT_FILE = "/home/port.txt"
- local PORT = 17
- local function readFile(path)
- if not fs.exists(path) then
- return false
- else
- local f_h = fs.open(path, "r")
- local rtn = f_h:read(fs.size(path))
- f_h:close()
- return rtn
- end
- end
- local function writeFile(path, data_string)
- local f_h = fs.open(path, "w")
- f_h:write(data_string)
- f_h:close()
- end
- local function getMessage()
- local _, _, from, port, _, message = event.pull("modem_message")
- return message
- end
- local function sendMessage(msg)
- m.broadcast(PORT, msg)
- end
- local function sendFile(fileName, fileRaw) -- Copy this function to programs that will communicate with this one. Also copy the getMessage and sendMessage functions.
- local file_length = fileRaw:len()
- local sent_amount = 0
- sendMessage("download:start")
- getMessage()
- sendMessage(fileName)
- getMessage()
- while sent_amount < file_length do
- local send_length = math.min(file_length, sent_amount + 500)
- local send_string = fileRaw:sub(sent_amount + 1, send_length)
- sent_amount = send_length
- sendMessage(send_string)
- print("Sending File (" .. tostring(file_length - sent_amount) .. " bytes remaining)")
- getMessage()
- end
- print("File sent!")
- sendMessage("download:end")
- end
- local function downloadFile()
- sendMessage("download:name")
- local file_name = getMessage()
- local file_raw = ""
- sendMessage("download:next")
- while true do
- local msg = getMessage()
- if msg == "download:end" then
- break
- else
- file_raw = file_raw .. msg
- end
- sendMessage("download:next")
- end
- writeFile("/home/" .. file_name, file_raw)
- end
- local argv = {...}
- local function init_rc()
- local rc_path = shell.resolve(process.info().path)
- if not string.find(rc_path, ".lua") then
- rc_path = rc_path .. ".lua"
- end
- writeFile("/home/.shrc", rc_path)
- writeFile(PORT_FILE, argv[2])
- end
- if argv[1] == "init" then
- PORT = tonumber(argv[2])
- init_rc()
- else
- if fs.exists(PORT_FILE) then
- PORT = tonumber(readFile(PORT_FILE))
- end
- end
- m.open(PORT)
- m.setStrength(64)
- while true do
- local msg = getMessage()
- if msg == "download:start" then
- downloadFile()
- elseif msg == "execute" then
- sendMessage("execute:command")
- shell.execute(getMessage())
- elseif msg == "exit" then
- break
- end
- end
- print("Remote Control Terminated.")
Add Comment
Please, Sign In to add comment