--A simple remote Lua shell. Displays command prompt, supports "=" prefix --and has initial support for print/io.stdout redirection to the network --connection. For convenience, I recommend you to install rlwrap and connect --to the host like this: -- -- rlwrap ncat somehost -- --This will give you convenient line editing and history, just like in the --real interpreter. --Couldn't simply edit metatable of io.stdout - there's no Lua-side interface --for that. old_stdout = io.stdout io.stdout = { write = function (self, data) if lastconn ~= old_stdout then lastconn.super:send(data) else old_stdout:write(data) end end, } function print (...) for k, v in pairs { ..., } do io.stdout:write(tostring(v) .. "\t") end io.stdout:write "\n" end function eval (line) if line:sub(1, 1) == "=" then line = "return " .. line:sub(2) end local results = { pcall(load(line)), } local tosend = "" for _, v in pairs { table.unpack(results, 2, results.n), } do tosend = tosend .. (tostring(v)) .. "\t" end if tosend == "" then tosend = "nil" else tosend = tosend:sub(1, -2) end tosend = tosend .. "\n> " return tosend end showed_prompt = false return { connect = function (self, host, port) local ret = self.super:connect(host, port) if not showed_prompt then old_stdout:write "> " old_stdout:flush() showed_prompt = true end self.super:send "> " return ret end, recv = function (self) local line, err = self.super:recv() if line == nil then return line, err end lastconn = self local tosend = eval(line) self.super:send(tosend) return "" end, send = function (self, str) if last_sent == str then return "" else last_sent = str end lastconn = old_stdout old_stdout:write(eval(str)) old_stdout:flush() return "" end, }