Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local TEMP_FILE_PATH = "/scripts/ssh/.temp"
- local localPrint = print
- local oldPull = os.pullEvent
- os.pullEvent = os.pullEventRaw
- print = myPrint
- function myPrint(str)
- local file = io.open(TEMP_FILE_PATH, "a")
- file:write(str.."\n")
- file:close()
- end
- local activeConnections = {}
- function authenticateUser(username, password)
- return true
- end
- function waitForTermination()
- sleep(1)
- print("Enter the root password to terminate")
- while true do
- local password = read("*")
- if password == "teste" then
- localPrint("Session ended")
- return
- else
- localPrint("Wrong password")
- end
- end
- end
- function waitForConnections()
- local senderID, message = rednet.receive("ssh")
- local i = string.find(message, "|")
- if i ~= nil then
- local username = string.sub(message, 1, i-1)
- local password = string.sub(message, i+1)
- if authenticateUser(username, password) then
- rednet.send(senderID, {true, "Access granted"}, "ssh")
- localPrint("Client with id "..senderID.." connected")
- return senderID
- else
- rednet.send(senderID, {false,""}, "ssh")
- localPrint("Client with id "..senderID.." was not authorized")
- return waitForConnections()
- end
- else
- rednet.send(senderID, {false, "Wrong message format"}, "ssh")
- return waitForConnections()
- end
- end
- function shellServer()
- local clientID = nil
- rednet.open("left")
- localPrint("Running SSH host")
- clientID = waitForConnections()
- while true do
- local file = io.open(TEMP_FILE_PATH, "w")
- file:close()
- local senderID, command = rednet.receive("ssh")
- if senderID == clientID then
- --rednet.send(senderID, {true, "Access authorized"}, "ssh")
- shell.run(command)
- file = io.open(TEMP_FILE_PATH, "r")
- local content = file:read("*a")
- rednet.send(senderID, {true, content}, "ssh")
- else
- rednet.send(senderID, {false, "Access not authorized"}, "ssh")
- end
- end
- end
- parallel.waitForAny(waitForTermination, shellServer)
- print = localPrint
- os.pullEvent = oldPull
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement