-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmcssh.lua
60 lines (56 loc) · 1.94 KB
/
mcssh.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
local component=require("component")
local function myPrint(...)
return io.write(...)
end
function doRunSSH(sock)
local cmdLength=0
local cmdLengthTmpStr=""
local cmdLengthLeftBit=4
while true do
local s=sock.read(cmdLengthLeftBit)
if(s==nil) then return
else
cmdLengthTmpStr=cmdLengthTmpStr .. s
cmdLengthLeftBit=cmdLengthLeftBit-string.length(s)
if(cmdLengthLeftBit==0) then -- Receive 4B length
cmdLength=tonumber(cmdLengthTmpStr)
cmdLengthTmpStr="" -- Reset TmpStr
-- Start Data Receive
local cmdText=""
local done=0
while (done<cmdLength) do
s=sock.read(cmdLength-done)
if(s==nil) then return end-- Connection Closed.
done=done+string.length(s)
cmdText=cmdText .. s
end
-- Command received. Write to a temp file
-- This is because OC-Lua does not implements loadstring(). So we use dofile() instead
local fd=io.open("/tmp/a.lua","w")
fd:write(cmdText)
fd:close()
-- Replace output
local oldprint=print
print=oldprint
io.open("/tmp/a.txt")
-- Call dofile
dofile("/tmp/a.lua")
-- Switch back
io.close()
print=oldprint
-- Send Result back
local hand=io.open("/tmp/a.txt","r")
local resText=hand:read("a")
hand:close()
local resLen=tostring(string.length(resText))
sock.send(resLen)
sock.send(resText)
end
end
end
end
function runSSH(ServerIP,ServerPort)
local x=component.internet.connect(ServerIP,ServerPort)
doRunSSH(x)
x.close()
end