Advertisement
Guest User

remoteshell

a guest
Dec 19th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.03 KB | None | 0 0
  1. args = {...}
  2.  
  3. local editing = false
  4. local exit = false
  5.  
  6. function clear()
  7.   term.clear()
  8.   term.setCursorPos(1,1)
  9. end
  10.  
  11. for i,v in ipairs(peripheral.getNames()) do
  12.   if peripheral.getType(v) == "modem" then
  13.     rednet.open(v)
  14.   end
  15. end
  16.  
  17. if args[1] == nil then
  18.   term.setTextColor(colors.white)
  19.   print()
  20.   print("Usage: remoteshell <id>")
  21.   return
  22. end
  23.  
  24. local com = tonumber(args[1])
  25.  
  26. rednet.send(com, "print()")
  27. id,msg,d = rednet.receive(.3)
  28. if msg ~= nil and msg == "AUTHENTICATE" then
  29.   print()
  30.   print("AUTHENTICATION REQUIRED")
  31.   term.write("Username: ")
  32.   user = read()
  33.   term.write("Password: ")
  34.   pass = read("*")
  35.   rednet.send(com, {"AUTHENTICATE",user,pass})
  36.   id,msg,d = rednet.receive(.3)
  37.   if msg == nil then
  38.     return
  39.   end
  40. end
  41.  
  42. function listen()
  43.   id,msg = rednet.receive()
  44.   if msg == "AUTHENTICATE" then
  45.     error("YOU HAVE BEEN LOGGED OUT")
  46.   elseif msg == "breakpivot" then
  47.     print()
  48.     print("No pivot to break!")
  49.   elseif id == com then
  50.     if type(msg) == "table" then
  51.       if msg[1] == "ls" then
  52.         print()
  53.         print(textutils.serialise(msg[2]))
  54.       elseif msg[1] == "id" then
  55.         print()
  56.         print("HOST ID: "..msg[2])
  57.       elseif msg[1] == "peak" then
  58.         local file = fs.open(".t","w")
  59.         for i,v in ipairs(msg[2]) do
  60.           file.writeLine(v)
  61.         end
  62.         file.close()
  63.         editing = true
  64.         return
  65.       elseif msg[1] == "getconnected" then
  66.         print()
  67.         print(textutils.serialise(msg[2]))
  68.       end
  69.     else
  70.       loadstring(msg)()
  71.     end
  72.   end
  73. end
  74.  
  75. function split(s)
  76.   if s == "" then return {} end
  77.   local t = {}
  78.   for i in string.gmatch(s,"%S+") do
  79.     table.insert(t,i)
  80.   end
  81.   return t
  82. end
  83.  
  84. clear()
  85. term.setTextColor(colors.red)
  86. print("YOU ARE NOW IN A REMOTE CONNECTION WITH "..com)
  87. print("TYPE 'help' FOR HELP")
  88.  
  89. function start()
  90.   term.setTextColor(colors.red)
  91.   term.write("  : ")
  92.   term.setTextColor(colors.white)
  93.   local input1 = read()
  94.   local input = split(string.lower(input1))
  95.   if input == {} then
  96.     return
  97.   elseif input[1] == "help" then
  98.     print("remoteshell help")
  99.     print("---------------------")
  100.     term.setTextColor(colors.yellow)
  101.     term.write("print <data>")
  102.     term.setTextColor(colors.white)
  103.     print(": prints out data from the host computer on the client")
  104.    
  105.     term.setTextColor(colors.yellow)
  106.     term.write("id")
  107.     term.setTextColor(colors.white)
  108.     print(": prints the id of the connected host")
  109.    
  110.     term.setTextColor(colors.yellow)
  111.     term.write("ls")
  112.     term.setTextColor(colors.white)
  113.     print(": lists files on host computer")
  114.    
  115.     term.setTextColor(colors.yellow)
  116.     term.write("run <file>")
  117.     term.setTextColor(colors.white)
  118.     print(": runs specified file on host and prints the return val")
  119.    
  120.     term.setTextColor(colors.yellow)
  121.     term.write("peak <file>")
  122.     term.setTextColor(colors.white)
  123.     print(": peaks at a file")
  124.    
  125.     term.setTextColor(colors.yellow)
  126.     term.write("getconnected")
  127.     term.setTextColor(colors.white)
  128.     print(": prints computer ids connected to host")
  129.    
  130.     term.setTextColor(colors.yellow)
  131.     term.write("pivot <id>")
  132.     term.setTextColor(colors.white)
  133.     print(": remotely connects to the specified id")
  134.    
  135.     term.setTextColor(colors.yellow)
  136.     term.write("breakpivot")
  137.     term.setTextColor(colors.white)
  138.     print(": breaks pivot (:O)")
  139.    
  140.     term.setTextColor(colors.yellow)
  141.     term.write("rs on/off")
  142.     term.setTextColor(colors.white)
  143.     print(": turns all rs sides on or off")
  144.  
  145.     term.setTextColor(colors.yellow)
  146.     term.write("logout")
  147.     term.setTextColor(colors.white)
  148.     print(": logs out of remoteshell")
  149.   elseif input[1] == "logout" then
  150.     exit = true
  151.   elseif input[1] == "peak" then
  152.     if input[2] ~= nil then
  153.       rednet.send(com,{"peak",input[2]})
  154.     end
  155.   elseif input[1] == "rs" then
  156.     if input[2] == "on" then
  157.       rednet.send(com,{"rs","on"})
  158.     elseif input[2] == "off" then
  159.       rednet.send(com,{"rs","off"})
  160.     end
  161.   elseif input[1] == "id" then
  162.     rednet.send(com,{"id"})
  163.   elseif input[1] == "getconnected" then
  164.     rednet.send(com,{"getconnected"})
  165.   elseif input[1] == "pivot" then
  166.     if type(tonumber(input[2])) == "number" then
  167.       rednet.send(com,{"pivot",tonumber(input[2])})
  168.     end
  169.   elseif input[1] == "run" then
  170.     rednet.send(com, {"run",input[2]})
  171.   elseif input[1] == "ls" then
  172.     rednet.send(com,{"ls"})    
  173.   elseif input[1] == "print" then
  174.     local s = ""
  175.     for i,v in ipairs(input) do
  176.       if i == 2 then
  177.         s = v
  178.       elseif i ~= 1 then
  179.         s = s + " " + v
  180.       end
  181.     end
  182.     local send = "rednet.send("..os.getComputerID()..",'print('..textutils.serialise("..s..")..')')"
  183.     rednet.send(com, send)
  184.   else
  185.     rednet.send(com, input1)
  186.   end
  187. end
  188.  
  189. while true do
  190.   parallel.waitForAny(start,listen)
  191.   if editing then
  192.     shell.run("edit",".t")
  193.     clear()
  194.     fs.delete(".t")
  195.     editing = false
  196.   end
  197.   if exit then
  198.     clear()
  199.     break
  200.   end
  201. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement