-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCMD_base.lua
204 lines (185 loc) · 3.96 KB
/
CMD_base.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
local cmds_base={}
if (console_users == nil) then
console_users={}
end
function cmds_base.cat(ctx,filename)
fd = shell.open(ctx,filename, "r")
if fd == nil then
return -1
end
while true do
str=fd:readline()
if (str == nil) then
break
end
ctx.stdout:write(str)
end
fd:close()
return 0
end
local function console(ctx,onoff)
local users = 0
local old=false
for k,v in pairs(console_users) do
users=users+1
end
if (onoff) then
if (console_users[ctx] == nil) then
console_users[ctx]=1
users=users+1
if (users == 1) then
node.output(function(str) for key,item in pairs(console_users) do key.stderr:write(str) end end, 0)
end
else
old=true
end
else
if (console_users[ctx]) then
old=true
console_users[ctx]=nil
users=users-1
if (users == 0) then
node.output(nil)
end
end
end
return old,users
end
function cmds_base.console(ctx,onoff)
local old,users=console(ctx,onoff == "on")
ctx.stdout:print("Console users:",users)
end
function cmds_base.cp(ctx,src,dst)
local ret=0
fd1=shell.open(ctx,src,"r")
if (fd1 == nil) then
return -1
end
fd2=shell.open(ctx,dst,"w")
if (fd2) then
while true do
local data=fd1:read()
if (data == nil) then
break
end
if (fd2:write(data) == false) then
ctx.stderr:print("write error")
ret=-1
break
end
end
fd1:close()
fd2:close()
else
fd1:close()
ret=-1
end
return ret
end
function cmds_base.df(ctx)
remaining, used, total=file.fsinfo()
ctx.stdout:print("File system info:\nTotal : "..total.." (k)Bytes\nUsed : "..used.." (k)Bytes\nRemain: "..remaining.." (k)Bytes\n")
return 0
end
function cmds_base.dump(ctx,...)
local obj=_G
for i,v in ipairs(arg) do
ctx.stdout:print("Getting",v)
obj=obj[v]
if (type(obj) == "function" or type(obj) == "lightfunction") then
obj=debug.getinfo(obj)
end
end
if (type(obj) == "string" or type(obj) == "number") then
ctx.stdout:print(obj,type(obj))
else
for key,item in pairs(obj) do
ctx.stdout:print(key,type(item))
end
end
return 0
end
function cmds_base.free(ctx)
local old=collectgarbage("count")
collectgarbage("collect")
ctx.stdout:print(node.heap(),old*1024,collectgarbage("count")*1024)
return 0
end
function cmds_base.ls(ctx)
local files=file.list()
local sfiles={}
for key,value in pairs(files) do
table.insert(sfiles,key)
end
table.sort(sfiles)
for i,value in ipairs(sfiles) do
ctx.stdout:print(value,files[value])
end
return 0
end
function cmds_base.lua(ctx,...)
local old,users=console(ctx,true)
local s=table.concat(arg," ")
loadstring(s)()
console(ctx,old)
return 0
end
function cmds_base.md5sum(ctx,file)
local hash=crypto.new_hash("MD5")
fd = shell.open(ctx,filename, "r")
if fd == nil then
return -1
end
while true do
local data=fd:read()
if (data == nil) then
break
end
hash:update(data)
end
ctx.stdout:print(encoder.toHex(hash:finalize()))
return 0
end
function cmds_base.mv(ctx,old,new)
if (file.rename(old,new)) then
return 0
end
ctx.stderr.print("Failed to rename '"+old+"' to '"+new+"'")
return -1
end
function cmds_base.print(ctx,...)
ctx.stdout:print(...)
return 0
end
function cmds_base.wifiscan(ctx,...)
wifi.sta.scan({ hidden = 1 }, function(err,arr)
if err then
ctx.stderr:print ("Scan failed:", err)
else
ctx.stdout:print("\n", string.format("%-26s","SSID"),"Channel BSSID RSSI Auth Bandwidth")
for i,ap in ipairs(arr) do
ctx.stdout:print(string.format("%-32s",ap.ssid),ap.channel,ap.bssid,ap.rssi,ap.auth,ap.bandwidth)
end
ctx.stdout:print("-- Total APs: ", #arr)
end
end)
return 0
end
function cmds_base.reboot(ctx)
node.restart()
return 0
end
function cmds_base.rm(ctx,name)
file.remove(name)
return 0
end
function cmds_base.uptime(ctx)
local us=node.uptime()
local ms=math.floor(us/1000)
us=us%1000
local s=math.floor(ms/1000)
ms=ms%1000
ctx.stdout:print(s,"s",ms,"ms",us,"us")
return 0
end
return cmds_base