This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Dean4Devil's profile picture

[1.3/1.4] SSH-like Remote Control

Started by Dean4Devil, 21 October 2012 - 12:43 PM
Dean4Devil #1
Posted 21 October 2012 - 02:43 PM
Remote control you computers and turtles!


As the title says, you can use this program to remotly control all your PCs and turtles via Rednet.

Use special commands like:
- /connect to connect to your turtle/pc
- /disconnect to reset the console
- /exit to exit the program
many more to come :)/>/>

This uses the fs API, so you have to write shell.run("tunnel", 5) instead of just "tunnel 5".

Code:
SpoilerClient:

local version = "Version 1.2"
local description = "'SSH'-Client for connecting to a remote controlable PC/Turtle via Rednet"
old_commands = {}
running = true
side = "left"	--Side where the Modem is attached on
w, h = term.getSize()
Send_ID = 0
send_to = 0
nbr_com = 0
commandSpec = ""
--Functions
local function printCentered(str, ypos)
term.setCursorPos(w/2 - #str/2, ypos)
term.write(str)
end
local function printRight(str, ypos)
term.setCursorPos(w - #str, ypos)
term.write(str)
end
local function printLeft(str, ypos)
term.setCursorPos(1, ypos)
term.write(str)
end
function drawHeader()
printCentered("SSH-System for Rednet.", 1)
printCentered(string.rep("-", w), 2)
printLeft(version, h)
printRight("by Dean4Devil", h)
end

function ssh_send(msg)
rednet.open(side)
rednet.send(send_to, msg)
id, answer, distance = rednet.receive(5)
answer = tostring(answer)
rednet.close(side)
end
--Funktions END
--Main
term.clear()
while running == true do
term.clear()
drawHeader()
term.setCursorPos(1, 4)
write( "> " )
commandRaw = read()
commandMain = tostring(commandRaw)
table.insert(old_commands, commandMain)
commandMainLower = string.lower(commandMain)
commandSpec = string.sub(commandMain, 1, 1)
if commandSpec == "/" then   --lokales Kommando -> nicht versenden!
  if commandMainLower == "/exit" then
   running = false
  elseif commandMainLower == "/connect" then
   print("Give ID:")
   write("> ")
   send_to = tonumber(read())
  elseif commandMainLower == "/disconnect" then
   term.clear()
   drawHeader()
   send_to = 0
  elseif commandMainLower == "/dito" then
   write("n")
   write(old_commands[#old_commands-1])
   ssh_send(old_commands[#old_commands-1])
  else
   print("Unknown Command!")
  end
elseif commandSpec == "#" then  --Server-Präprozessor -> versenden

else		--normale Kommandos -> versenden
  ssh_send(commandMain)
  print(answer)
  sleep(1)
end

end
term.clear()
term.setCursorPos(1,1)
--Main END

Server:

local version = "Version 1.2"
local description = "Rednet 'SSH'-Sender for Turtles and Computers."
id = 0
running = true
side = "right" --Side of the Modem
--Functions
local function receive_instruction()
rednet.open(side)
while true do
  id, commandRaw, distance = rednet.receive()
   commandMain = tostring(commandRaw)
   id = tonumber(id)
  
  --Auswertung
  write(id)
  write("  ")
  write(commandMain)
  write("  ")
  exe = fs.open("ssh_temp.lua", "w")
  exe.write(commandMain)
  exe.close()
  answerRaw = shell.run("ssh_temp.lua")
  answer = tostring(answerRaw)
  print(answer)
  rednet.send(id, answer)

end
rednet.close(side)
end
--Functions END
--Main
while running do
receive_instruction()
end
--Main END
74kumi #2
Posted 15 November 2012 - 11:57 AM
some instructions on how to set this up would be nice or some comments in the code for what to edit
prasselpikachu #3
Posted 25 November 2012 - 01:03 PM
So is this like real SSH? I mean, does it display exact the same screen as the controlled computer? Does it look EXACTLY as if you were sitting in front of the controlled computer? If yes, I am really impressed. I wanted to do something like this too, but i'm still waiting for an example on how to basically do that by AfterLifeLochie (a really busy Guy ^^)

Also I would remove those german comments in the client code (Keine Angst, ich bin auch deutsch :P/>/>) if you don't want to let everyone see you're german :D/>/>