Posted 24 November 2012 - 04:43 AM
Very hacked together telnet client for computer craft with the sockcraft mod installed. It's a work in progress, and probably isn't actually a true telnet client, but more of a raw socket client. I don't have an actual telnet server interface handy, so not sure if that part actually works. It will connect to a web server, and given the proper input, you can get a web server to send raw html back over the connection.
It expects the ip to be the first argument, then accepts a port number as an optional argument, and finally optionally accepts the side of the computer that the WebInterface is connected to. If the port is not given, it defaults to 23. If the side is not supplied, it searches all 6 sides and uses whatever web interface it can find.
It expects the ip to be the first argument, then accepts a port number as an optional argument, and finally optionally accepts the side of the computer that the WebInterface is connected to. If the port is not given, it defaults to 23. If the side is not supplied, it searches all 6 sides and uses whatever web interface it can find.
--Telnet.lua
--Jonathan Bennett 2012
--GPL v3
local tArgs = {...}
local test1 = os.time()..""
local history = {}
local side
local port = "23"
if (tArgs[1] == nil) or tArgs[1] == "help" then
print("usage: telnet ip address [port] [side WebInterface is connected to] \n")
end
local ip = tArgs[1]
---------------------------functions-----------------
local function readLine()
while true do
e, p1, p2 = os.pullEvent()
if e == "socket_line_received" then
print(p2.."\n")
elseif e == "socket_remote_closed" then
socket.unregisterHandle(test1)
return nil
end
end
end
local function readInput()
while true do
local rd = read(nil, history)
socket.writeLine(test1, rd)
end
end
---------------------------------------------------------
if peripheral.getType("left") == "WebInterface" then side = "left" end
if peripheral.getType("right") == "WebInterface" then side = "right" end
if peripheral.getType("top") == "WebInterface" then side = "top" end
if peripheral.getType("bottom") == "WebInterface" then side = "bottom" end
if peripheral.getType("front") == "WebInterface" then side = "front" end
if peripheral.getType("back") == "WebInterface" then side = "back" end
if side == "" then
print "no WebInterface detected \n"
return 0
end
if tArgs[2] ~= nil then
local arg2 = tArgs[2]
if (((arg2 == "left") or (arg2 == "right")) or ((arg2 == "top") or (arg2 == "bottom"))) or ((arg2 == "front") or (arg2 == "back")) then
side = arg2
else
port = arg2
if tArgs[3] ~= nil then
side = tArgs[3]
end
end
end
socket = peripheral.wrap(side)
socket.registerHandle(test1.."")
socket.subscribeEvent(test1, "text")
suc = socket.connect(test1, ip, port, "false")
if suc == 0 then print("connected to:"..ip)
elseif sud == -5 then
print("Can't connect, host unavailable \n")
return 0
else print("unknown error: "..suc.."\n")
return 0
end
parallel.waitForAny(readLine, readInput)