- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmcssh.lua
More file actions
Latest commit
60 lines (56 loc) · 1.94 KB
/
Copy pathmcssh.lua
File metadata and controls
60 lines (56 loc) · 1.94 KB
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
localcomponent=require("component")
localfunctionmyPrint(...)
returnio.write(...)
end
functiondoRunSSH(sock)
localcmdLength=0
localcmdLengthTmpStr=""
localcmdLengthLeftBit=4
whiletruedo
locals=sock.read(cmdLengthLeftBit)
if(s==nil) thenreturn
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
localcmdText=""
localdone=0
while (done<cmdLength) do
s=sock.read(cmdLength-done)
if(s==nil) thenreturnend-- 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
localfd=io.open("/tmp/a.lua","w")
fd:write(cmdText)
fd:close()
-- Replace output
localoldprint=print
print=oldprint
io.open("/tmp/a.txt")
-- Call dofile
dofile("/tmp/a.lua")
-- Switch back
io.close()
print=oldprint
-- Send Result back
localhand=io.open("/tmp/a.txt","r")
localresText=hand:read("a")
hand:close()
localresLen=tostring(string.length(resText))
sock.send(resLen)
sock.send(resText)
end
end
end
end
functionrunSSH(ServerIP,ServerPort)
localx=component.internet.connect(ServerIP,ServerPort)
doRunSSH(x)
x.close()
end