mirror of
https://github.com/JamesonHuang/OpenWrt_Luci_Lua.git
synced 2024-11-23 22:00:11 +00:00
art_wifi_third, watchcat
Signed-off-by: JamesonHuang <462430725@qq.com>
This commit is contained in:
parent
813f8d93e5
commit
a3c0fbb62f
BIN
mind_map/定时Wifi开关.xmind
Normal file
BIN
mind_map/定时Wifi开关.xmind
Normal file
Binary file not shown.
87
smart_wifi_code/nwfs.lua
Normal file
87
smart_wifi_code/nwfs.lua
Normal file
@ -0,0 +1,87 @@
|
||||
--set
|
||||
function fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat)
|
||||
if week_repeat == "non-repeat" then
|
||||
week_repeat = "0,1,2,3,4,5,6" --"1,2,3,4,5,6,0 is error,可能会误删repeat的数据"
|
||||
cmd_close = "/sbin/smart_wifi "..close_min.." "..close_hour.." "..week_repeat
|
||||
cmd_open = "/sbin/smart_wifi "..open_min.." "..open_hour.." "..week_repeat
|
||||
else
|
||||
cmd_close = "echo '"..close_min.." "..close_hour.." * * "..week_repeat.." /sbin/wifi down "..wnet..";"
|
||||
..close_min..close_hour..open_min..open_hour..week_repeat
|
||||
..">/dev/null 2>/dev/null' >> /etc/crontabs/root; crontab /etc/crontabs/root;"
|
||||
|
||||
cmd_open = "echo '"..open_min.." "..open_hour.." * * "..week_repeat.." /sbin/wifi up "..wnet..";"
|
||||
..close_min..close_hour..open_min..open_hour..week_repeat
|
||||
..">/dev/null 2>/dev/null' >> /etc/crontabs/root; echo '' >> /etc/crontabs/root; crontab /etc/crontabs/root;"
|
||||
end
|
||||
luci.http.write_json(cmd_close)
|
||||
luci.http.write_json(cmd_open)
|
||||
|
||||
exec_cmd_in_sh(cmd_close)
|
||||
posix.sleep(1)
|
||||
exec_cmd_in_sh(cmd_open)
|
||||
end
|
||||
|
||||
--stop
|
||||
function set_smart_wifi_stop(update_flag)
|
||||
local cmd = "sed -i '/"..update_flag.."/d' /etc/crontabs/root; crontab /etc/crontabs/root"
|
||||
exec_cmd_in_sh(cmd)
|
||||
luci.http.write_json(cmd)
|
||||
end
|
||||
|
||||
|
||||
--function: 定时wifi开关shell形式
|
||||
--author: rh_Jameson
|
||||
function set_smart_wifi_updown()
|
||||
|
||||
--get para
|
||||
--wnet = luci.http.formvalue("wnet")
|
||||
--close_hour = luci.http.formvalue("close_hour")
|
||||
--close_min = luci.http.formvalue("close_min")
|
||||
--open_hour = luci.http.formvalue("open_hour")
|
||||
--open_min = luci.http.formvalue("open_min")
|
||||
--repeat_var_from_http = luci.http.formvalue("repeat_var")
|
||||
--func = luci.http.formvalue("func")
|
||||
|
||||
--test
|
||||
local wnet = 'mt7628'
|
||||
local close_hour = "07"
|
||||
local close_min = "25"
|
||||
local open_hour = "07"
|
||||
local open_min = "26"
|
||||
local repeat_var_from_http = "1111111"
|
||||
local func = luci.http.formvalue("func")
|
||||
--para err manage
|
||||
if string.len(repeat_var_from_http) ~= 7 then
|
||||
luci.http.write_json("len error")
|
||||
return
|
||||
end
|
||||
|
||||
--get repeat
|
||||
local week_repeat = ""
|
||||
for i = 0, #repeat_var_from_http do
|
||||
tmp = string.sub(repeat_var_from_http, i, i)
|
||||
if tmp == "1" then
|
||||
if i == #repeat_var_from_http then
|
||||
week_repeat = week_repeat..0
|
||||
break
|
||||
end
|
||||
week_repeat = week_repeat..tostring(i)..","
|
||||
end
|
||||
end
|
||||
if week_repeat == "" then
|
||||
week_repeat = "non-repeat"
|
||||
end
|
||||
--exec
|
||||
if func == "set" then
|
||||
fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat)
|
||||
elseif func == "stop" or func == "update" then
|
||||
local update_flag = luci.http.formvalue("flag")
|
||||
set_smart_wifi_stop(update_flag)
|
||||
|
||||
if func == "update" then
|
||||
posix.sleep(1)
|
||||
fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
101
smart_wifi_code/nwfs.lua~
Normal file
101
smart_wifi_code/nwfs.lua~
Normal file
@ -0,0 +1,101 @@
|
||||
function set_wifi_up()
|
||||
--local cmd = "/sbin/wifi up".. wnet
|
||||
local cmd = "/sbin/wifi up ".. "mt7628"
|
||||
exec_cmd_in_sh(cmd)
|
||||
luci.http.write_json("true")
|
||||
end
|
||||
|
||||
function set_wifi_down()
|
||||
local cmd = "/sbin/wifi down ".."mt7628"
|
||||
--local cmd = "/sbin/wifi down"..wnet
|
||||
exec_cmd_in_sh(cmd)
|
||||
luci.http.write_json("true")
|
||||
end
|
||||
|
||||
--set
|
||||
function fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat)
|
||||
if week_repeat == "non-repeat" then
|
||||
week_repeat = "0,1,2,3,4,5,6" --"1,2,3,4,5,6,0 is error,可能会误删repeat的数据"
|
||||
cmd_close = "/sbin/smart_wifi "..close_min.." "..close_hour.." "..week_repeat
|
||||
cmd_open = "/sbin/smart_wifi "..open_min.." "..open_hour.." "..week_repeat
|
||||
else
|
||||
cmd_close = "echo '"..close_min.." "..close_hour.." * * "..week_repeat.." /sbin/wifi down "..wnet..";"
|
||||
..close_min..close_hour..open_min..open_hour..week_repeat
|
||||
..">/dev/null 2>/dev/null' >> /etc/crontabs/root; crontab /etc/crontabs/root;"
|
||||
|
||||
cmd_open = "echo '"..open_min.." "..open_hour.." * * "..week_repeat.." /sbin/wifi up "..wnet..";"
|
||||
..close_min..close_hour..open_min..open_hour..week_repeat
|
||||
..">/dev/null 2>/dev/null' >> /etc/crontabs/root; echo '' >> /etc/crontabs/root; crontab /etc/crontabs/root;"
|
||||
end
|
||||
luci.http.write_json(cmd_close)
|
||||
luci.http.write_json(cmd_open)
|
||||
|
||||
exec_cmd_in_sh(cmd_close)
|
||||
posix.sleep(1)
|
||||
exec_cmd_in_sh(cmd_open)
|
||||
end
|
||||
|
||||
--stop
|
||||
function set_smart_wifi_stop(update_flag)
|
||||
local cmd = "sed -i '/"..update_flag.."/d' /etc/crontabs/root; crontab /etc/crontabs/root"
|
||||
exec_cmd_in_sh(cmd)
|
||||
luci.http.write_json(cmd)
|
||||
end
|
||||
|
||||
|
||||
--function: 定时wifi开关shell形式
|
||||
--author: rh_Jameson
|
||||
function set_smart_wifi_updown()
|
||||
|
||||
--get para
|
||||
--wnet = luci.http.formvalue("wnet")
|
||||
--close_hour = luci.http.formvalue("close_hour")
|
||||
--close_min = luci.http.formvalue("close_min")
|
||||
--open_hour = luci.http.formvalue("open_hour")
|
||||
--open_min = luci.http.formvalue("open_min")
|
||||
--repeat_var_from_http = luci.http.formvalue("repeat_var")
|
||||
--func = luci.http.formvalue("func")
|
||||
|
||||
--test
|
||||
local wnet = 'mt7628'
|
||||
local close_hour = "07"
|
||||
local close_min = "25"
|
||||
local open_hour = "07"
|
||||
local open_min = "26"
|
||||
local repeat_var_from_http = "1111111"
|
||||
local func = luci.http.formvalue("func")
|
||||
--para err manage
|
||||
if string.len(repeat_var_from_http) ~= 7 then
|
||||
luci.http.write_json("len error")
|
||||
return
|
||||
end
|
||||
|
||||
--get repeat
|
||||
local week_repeat = ""
|
||||
for i = 0, #repeat_var_from_http do
|
||||
tmp = string.sub(repeat_var_from_http, i, i)
|
||||
if tmp == "1" then
|
||||
if i == #repeat_var_from_http then
|
||||
week_repeat = week_repeat..0
|
||||
break
|
||||
end
|
||||
week_repeat = week_repeat..tostring(i)..","
|
||||
end
|
||||
end
|
||||
if week_repeat == "" then
|
||||
week_repeat = "non-repeat"
|
||||
end
|
||||
--exec
|
||||
if func == "set" then
|
||||
fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat)
|
||||
elseif func == "stop" or func == "update" then
|
||||
local update_flag = luci.http.formvalue("flag")
|
||||
set_smart_wifi_stop(update_flag)
|
||||
|
||||
if func == "update" then
|
||||
posix.sleep(1)
|
||||
fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
4
smart_wifi_code/smart_wifi
Executable file
4
smart_wifi_code/smart_wifi
Executable file
@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
echo "" >> /etc/crontabs/root;
|
||||
echo "$1 $2 * * $3 /sbin/wifi down mt7628;sleep 4; sed -i '/$1$2$3/d' /etc/crontabs/root; crontab /etc/crontab/root; $1$2$3 >/dev/null 2>/dev/null " >> /etc/crontabs/root; crontab /etc/crontabs/root;
|
||||
echo "" >> /etc/crontabs/root;
|
24
smart_wifi_code/test.lua
Normal file
24
smart_wifi_code/test.lua
Normal file
@ -0,0 +1,24 @@
|
||||
local nwfs = require "meizu.nwfs"
|
||||
|
||||
--error
|
||||
--nwfs.fork_smart_wifi_updown("mt7628", "*", "*", "*", "*", "*")
|
||||
|
||||
|
||||
|
||||
|
||||
--set repeat task test
|
||||
--nwfs.fork_smart_wifi_updown("mt7628", "22", "22", "22", "23", "1,4,5,0")
|
||||
--posix.sleep(2)
|
||||
--nwfs.fork_smart_wifi_updown("mt7628", "12", "09", "22", "23", "1,2,3,4,5,6,0")
|
||||
|
||||
|
||||
|
||||
--stop all task
|
||||
nwfs.set_smart_wifi_stop("*****")
|
||||
|
||||
|
||||
|
||||
--set non-repeat task test
|
||||
--nwfs.fork_smart_wifi_updown("mt7628", "12", "06", "22", "23", "non-repeat")
|
||||
--posix.sleep(1)
|
||||
--nwfs.fork_smart_wifi_updown("mt7628", "22", "11", "11", "22", "non-repeat")
|
554
smart_wifi_code/updateWifi.lua
Normal file
554
smart_wifi_code/updateWifi.lua
Normal file
@ -0,0 +1,554 @@
|
||||
==============================第一版=============================
|
||||
--function: 定时wifi开关
|
||||
--author: rh_Jameson
|
||||
function smart_wifi_shutdown()
|
||||
local wnet = 'mt7628.network1'
|
||||
local table = {}
|
||||
|
||||
--get para
|
||||
--local close_time = luci.http.formvalue("close_time")
|
||||
--local open_time = luci.http.formvalue("open_time")
|
||||
|
||||
--test normal
|
||||
--local close_time = os.time() + 5
|
||||
--local restart_time = os.time() + 10
|
||||
|
||||
--test exception
|
||||
local close_time = os.time() - 5
|
||||
local restart_time = os.time() - 10
|
||||
|
||||
--para err manage
|
||||
if close_time < os.time() or restart_time < close_time then
|
||||
table["err"] = true
|
||||
luci.http.write_json(table)
|
||||
return
|
||||
end
|
||||
--do close
|
||||
while true do
|
||||
if os.time() ~= close_time then
|
||||
posix.sleep(1)
|
||||
else
|
||||
wifi_shutdown(wnet)
|
||||
table["close"] = true
|
||||
luci.http.write_json(table)
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
--do restart
|
||||
while true do
|
||||
if os.time() ~= restart_time then
|
||||
posix.sleep(1)
|
||||
else
|
||||
wifi_reconnect(wnet)
|
||||
table["restart"] = true
|
||||
luci.http.write_json(table)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
============================第二版================================
|
||||
|
||||
|
||||
-----------------------api/index.lua------------------------------
|
||||
set_smart_wifi_updown = nwfs.set_smart_wifi_updown
|
||||
set_wifi_up = nwfs.set_wifi_up
|
||||
set_wifi_down = nwfs.set_wifi_down
|
||||
set_smart_wifi_update = nwfs.set_smart_wifi_update
|
||||
set_smart_wifi_stop = nwfs.set_smart_wifi_stop
|
||||
|
||||
--rh
|
||||
page = entry({"api", "wifiUp"}, call("set_wifi_up"), nil)
|
||||
page.leaf = true
|
||||
page = entry({"api", "wifiDown"}, call("set_wifi_down"), nil)
|
||||
page.leaf = true
|
||||
page = entry({"api", "setSmartWifiUpdate"}, call("set_smart_wifi_update"), nil)
|
||||
page.leaf = true
|
||||
page = entry({"api", "setSmartWifiStop"}, call("set_smart_wifi_stop"), nil)
|
||||
page.leaf = true
|
||||
page = entry({"api", "setSmartWifiUpdown"}, call("set_smart_wifi_updown"), nil)
|
||||
page.leaf = true
|
||||
|
||||
|
||||
|
||||
|
||||
------------------------meizu/nwfs.lua----------------------------
|
||||
function set_wifi_up()
|
||||
--local cmd = "/sbin/wifi up".. wnet
|
||||
local cmd = "/sbin/wifi up ".. "mt7628"
|
||||
exec_cmd_in_sh(cmd)
|
||||
luci.http.write_json("true")
|
||||
end
|
||||
|
||||
function set_wifi_down()
|
||||
local cmd = "/sbin/wifi down ".."mt7628"
|
||||
--local cmd = "/sbin/wifi down"..wnet
|
||||
exec_cmd_in_sh(cmd)
|
||||
luci.http.write_json("true")
|
||||
end
|
||||
|
||||
function set_smart_wifi_stop()
|
||||
local pid = luci.http.formvalue("pid")
|
||||
--exception manage
|
||||
if(close_time == nil or open_time == nil) then
|
||||
luci.http.write_json("smart_wifi not set")
|
||||
return
|
||||
end
|
||||
if(pid == nil) then
|
||||
luci.http.write_json("pid err")
|
||||
end
|
||||
|
||||
--到点关闭前
|
||||
if os.time() < close_time then
|
||||
local cmd = "kill "..pid
|
||||
--到点关闭后,到点启动前
|
||||
elseif os.time() >= close_time and os.time() <= open_time then
|
||||
local cmd = "kill "..pid.."; /sbin/wifi up"
|
||||
--到点启动后
|
||||
else
|
||||
luci.http.write_json("smart wifi has finished,can not stop!")
|
||||
return
|
||||
end
|
||||
exec_cmd_in_sh(cmd)
|
||||
luci.http.write_json("true")
|
||||
end
|
||||
|
||||
function set_smart_wifi_update()
|
||||
local pid = luci.http.formvalue("pid")
|
||||
|
||||
--exception manage
|
||||
if(close_time == nil or open_time == nil) then
|
||||
luci.http.write_json("smart_wifi not set")
|
||||
return
|
||||
end
|
||||
if(pid == nil) then
|
||||
luci.http.write_json("pid err")
|
||||
end
|
||||
|
||||
--到点关闭前
|
||||
if os.time() < close_time then
|
||||
local cmd = "kill "..pid
|
||||
--到点关闭后,到点启动前
|
||||
elseif os.time() >= close_time and os.time() <= open_time then
|
||||
local cmd = "kill "..pid.."; /sbin/wifi up"
|
||||
--到点启动后
|
||||
end
|
||||
exec_cmd_in_sh(cmd)
|
||||
|
||||
--get para
|
||||
--close_time = os.time(lucimZhttp.formvalue("close_time"))
|
||||
--open_time = os.time(luci.http.formvalue("open_time"))
|
||||
|
||||
--test normal
|
||||
close_time = os.time() + 5
|
||||
restart_time = os.time() + 10
|
||||
|
||||
--test exception
|
||||
--close_time = os.time() - 5
|
||||
--restart_time = os.time() - 10
|
||||
|
||||
set_smart_wifi_updown(close_time, restart_time)
|
||||
end
|
||||
|
||||
|
||||
function fork_smart_wifi_updown( wnet, close_time, open_time)
|
||||
local close_interval = close_time - os.time()
|
||||
local open_interval = open_time - os.time()
|
||||
local cmd = string.format("sleep 15;/sbin/wifi down "..wnet.."; sleep 15; /sbin/wifi up "..wnet)
|
||||
|
||||
return exec_cmd_in_sh(cmd)
|
||||
--exec_cmd_in_sh(cmd)
|
||||
end
|
||||
|
||||
|
||||
--function: 定时wifi开关shell形式
|
||||
--author: rh_Jameson
|
||||
function set_smart_wifi_updown()
|
||||
local wnet = 'mt7628'
|
||||
local info = {}
|
||||
|
||||
--get para
|
||||
--close_time = lucimZhttp.formvalue("close_time")
|
||||
--open_time = luci.http.formvalue("open_time")
|
||||
|
||||
--test normal
|
||||
close_time = os.time() + 5
|
||||
restart_time = os.time() + 10
|
||||
|
||||
--test exception
|
||||
--close_time = os.time() - 5
|
||||
--restart_time = os.time() - 10
|
||||
|
||||
--para err manage
|
||||
if close_time < os.time() or restart_time < close_time then
|
||||
info["SUCCESS"] = false
|
||||
else
|
||||
info["SUCCESS"] = true
|
||||
end
|
||||
|
||||
local pid = fork_smart_wifi_updown(wnet, close_time, restart_time)
|
||||
info["PID"] = pid
|
||||
luci.http.write_json(info)
|
||||
end
|
||||
--重载
|
||||
function set_smart_wifi_updown(close_time,restart_time)
|
||||
|
||||
local wnet = 'mt7628'
|
||||
local info = {}
|
||||
|
||||
--para err manage
|
||||
if close_time < os.time() or restart_time < close_time then
|
||||
info["SUCCESS"] = false
|
||||
else
|
||||
info["SUCCESS"] = true
|
||||
end
|
||||
|
||||
local pid = fork_smart_wifi_updown(wnet, close_time, restart_time)
|
||||
info["PID"] = pid
|
||||
luci.http.write_json(info)
|
||||
end
|
||||
|
||||
|
||||
-------------------------------bfs.lua--line_92-------------------------------
|
||||
function exec_cmd_in_sh(command)
|
||||
local nio = require("nixio")
|
||||
require "MZLog".log(3, command)
|
||||
local pid = nio.fork()
|
||||
if pid > 0 then
|
||||
return pid
|
||||
elseif pid == 0 then
|
||||
nio.chdir("/")
|
||||
local null = nio.open("/dev/null", "w+")
|
||||
if null then
|
||||
nio.dup(null, nio.stderr)
|
||||
nio.dup(null, nio.stdout)
|
||||
nio.dup(null, nio.stdin)
|
||||
if null:fileno() > 2 then
|
||||
null:close()
|
||||
end
|
||||
end
|
||||
nio.exec("/bin/sh", "-c", command)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-------------------------UTC-CODE-字符串形式UTC时间转时间戳------------------------
|
||||
local lue = require("luci.util").exec
|
||||
local t = lue([[date -d "2010-10-10 10:10:10" +%s]])
|
||||
print(tonumber(t))
|
||||
|
||||
|
||||
local lue = require("luci.util").exec
|
||||
|
||||
|
||||
--local t = lue('date -d "2010-10-10 10:10:10" +%s')
|
||||
|
||||
local t = lue([[date -d "2010-10-10 10:10:10" +%s]])
|
||||
|
||||
print(tonumber(t))
|
||||
print(t)
|
||||
|
||||
|
||||
----------------------------Lua-多线程回调函数实现---------------------------------
|
||||
--回调
|
||||
function fork_exec_in_lua(func)
|
||||
local nio = require("nixio")
|
||||
-- require "MZLog".log(3, command)
|
||||
local pid = nio.fork()
|
||||
if pid > 0 then
|
||||
print(pid)
|
||||
return
|
||||
elseif pid == 0 then
|
||||
func()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
--主调
|
||||
local bfs = require("meizu.bfs")
|
||||
function test()
|
||||
local i = 1
|
||||
while(i < 10) do
|
||||
print(i)
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
|
||||
bfs.fork_exec_in_lua(test)
|
||||
|
||||
|
||||
|
||||
----------------------------Lua-函数不可以重载-----------------------------------
|
||||
--test
|
||||
function fun()
|
||||
print("11111")
|
||||
end
|
||||
|
||||
function fun(str)
|
||||
print(str)
|
||||
end
|
||||
|
||||
fun()
|
||||
fun("2222")
|
||||
fun()
|
||||
|
||||
|
||||
--result
|
||||
nil
|
||||
2222
|
||||
nil
|
||||
|
||||
----------------------------crontabs & crond------------------------------------
|
||||
|
||||
crontab -u //设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数
|
||||
crontab -l //列出某个用户cron服务的详细内容
|
||||
crontab -r //删除没个用户的cron服务
|
||||
crontab -e //编辑某个用户的cron服务
|
||||
|
||||
基本用法:
|
||||
1. crontab -l
|
||||
列出当前的crontab任务
|
||||
2. crontab -d
|
||||
删除当前的crontab任务
|
||||
3. crontab -e (solaris5.8上面是 crontab -r)
|
||||
编辑一个crontab任务,ctrl_D结束
|
||||
|
||||
|
||||
4. crontab filename
|
||||
以filename做为crontab的任务列表文件并载入
|
||||
|
||||
/*以下是多启动一个进程进行服务*/
|
||||
crond start //启动服务
|
||||
|
||||
crond stop //关闭服务
|
||||
|
||||
crond restart //重启服务
|
||||
|
||||
crond reload //重新载入配置
|
||||
|
||||
--openwrt设置/etc/crontabs/root:
|
||||
* * * * * echo "hello" >> /cron.txt
|
||||
|
||||
--每次做修改时使用crontab /etc/crondtabs/root
|
||||
--crontabs/root文件下写的定时任务是多线程运行,不按顺序执行
|
||||
|
||||
|
||||
Sed命令:
|
||||
sed -n '/hello/p' cron.txt 查询hello关键字,打印相应行
|
||||
|
||||
sed '/hello/d' cron.txt 删除含hello的行,输出到屏幕(原文件没删除)
|
||||
|
||||
sed -i '/hello/d' cron.txt 直接修改原文件
|
||||
|
||||
|
||||
|
||||
--[[non-repeat任务]]--
|
||||
* * * * * echo "flag non-repeat">> /cron.txt; sed -i '/sed/d' /etc/crontabs/root
|
||||
|
||||
--[[repeat任务]]--
|
||||
--添加定时任务exam
|
||||
echo "* * * * * date >>/cron.txt">>/etc/crontabs/root;flag >/dev/null 2>/
|
||||
|
||||
--删除定时任务exam
|
||||
sed -i '/hello/d' /etc/crontabs/root
|
||||
|
||||
--修改定时任务exam
|
||||
|
||||
==================================第三版-TEST================================
|
||||
|
||||
echo '* * * * non-repeat /sbin/wifi down mt7628; sed -i "/* * * * non-repeat/d" etc/crontabs/root' >> /etc/crontabs/root; crontab /etc/crontabs/root;
|
||||
|
||||
* * * * * /sbin/wifi down mt7628; sed -i '/sed/d' etc/crontabs/root; crontab /etc/crontabs/root;
|
||||
* * * * * date >>/cron.txt; sed -i '/sed/d' /etc/crontabs/root
|
||||
|
||||
|
||||
--non-repeat任务:
|
||||
echo "$1 $2 * * $3 /sbin/wifi down mt7628;
|
||||
sleep 4;
|
||||
sed -i '/$1$2$3/d' /etc/crontabs/root;
|
||||
crontab /etc/crontab/root;
|
||||
$1$2$3 >/dev/null 2>/dev/null " >> /etc/crontabs/root; crontab /etc/crontabs/root;
|
||||
|
||||
bin cron.txt * * dev /sbin/wifi down mt7628;sleep 4; sed -i '/bincron.txtdev/d' /etc/crontabs/root; crontab /etc/crontab/root; bincron.txtdev >/dev/null 2>/dev/null
|
||||
|
||||
|
||||
|
||||
--non-repeate任务:
|
||||
if week_repeat == "non-repeat" then
|
||||
week_repeat = "0,1,2,3,4,5,6"
|
||||
close_min = "40"
|
||||
Segmentation fault
|
||||
cmd = "/sbin/smart@OpenWrt:/usr/lib/lua/meizu# e_hour.." "..week_repeat
|
||||
else
|
||||
cmd = "echo '"..close_min.." "..close_hour.." * * "..week_repeat.." /sbin/wifi down "..wnet.."' >> /etc/crontabs/root; crontab /etc/cronta
|
||||
..close_min..close_hour..open_hour..open_min..week_repeat.." >/dev/null 2>/dev/null"
|
||||
--cmd = "echo '"..open_min.." "..open_hour.." * * "..week_repeat.." /sbin/wifi up "..wnet.."' >> /etc/crontabs/root; crontab /etc/crontabs
|
||||
end
|
||||
luci.http.write_json(cmd)
|
||||
exec_cmd_in_sh(cmd)
|
||||
luci.http.write_json(true)
|
||||
|
||||
|
||||
|
||||
if week_repeat == "non-repeat" then
|
||||
week_repeat = "*"
|
||||
cmd_close = "/sbin/smart_wifi "..close_hour.." "..close_min.." "..week_repeat
|
||||
--cmd_open = "echo '"..close_min.." "..close_hour.." * * "..week_repeat.." /sbin/wifi down "..wnet
|
||||
-- .."; sed -i '/"..close_min..close_hour..open_hour..open_min..week_repeat
|
||||
-- .."/d' etc/crontabs/root >> /etc/crontabs/root; crontab /etc/crontabs/root; "
|
||||
-- ..close_min..close_hour..open_hour..open_min..week_repeat.." >/dev/null 2>/dev/null"
|
||||
else
|
||||
cmd_close = "echo '"..close_min.." "..close_hour.." * * "..week_repeat.." /sbin/wifi down "..wnet
|
||||
.."' >> /etc/crontabs/root; crontab /etc/crontabs/root; "
|
||||
..close_min..close_hour..open_hour..open_min..week_repeat.." >/dev/null 2>/dev/null"
|
||||
|
||||
--cmd_open = "echo '"..open_min.." "..open_hour.." * * "..week_repeat.." /sbin/wifi up "..wnet.."' >> /etc/crontabs/root; crontab /etc/crontabs/root; "..close_min..close_hour..open_hour..open_min..week_repeat.." >/dev/null 2>/dev/null"
|
||||
end
|
||||
|
||||
luci.http.write_json(cmd_close)
|
||||
--luci.http.write_json(cmd_open)
|
||||
|
||||
exec_cmd_in_sh(cmd_close)
|
||||
--exec_cmd_in_sh(cmd_open)
|
||||
|
||||
luci.http.write_json(true)
|
||||
|
||||
|
||||
===============================================================第三版-succeed====================================================
|
||||
|
||||
-------------------------./package/base-files/files/sbin/wifi------------------------------------
|
||||
|
||||
#!/bin/sh
|
||||
echo "" >> /etc/crontabs/root;
|
||||
echo "$1 $2 * * $3 /sbin/wifi down mt7628;sleep 4; sed -i '/$1$2$3/d' /etc/crontabs/root; crontab /etc/crontab/root; $1$2$3 >/dev/null 2>/dev/null " >> /etc/crontabs/root; crontab /etc/crontabs/root;
|
||||
echo "" >> /etc/crontabs/root;
|
||||
|
||||
|
||||
|
||||
------------------------------------------------set smart wifi------------------------------------
|
||||
function fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat)
|
||||
if week_repeat == "non-repeat" then
|
||||
week_repeat = "0,1,2,3,4,5,6" --"1,2,3,4,5,6,0 is error,可能会误删repeat的数据"
|
||||
cmd_close = "/sbin/smart_wifi "..close_min.." "..close_hour.." "..week_repeat
|
||||
cmd_open = "/sbin/smart_wifi "..open_min.." "..open_hour.." "..week_repeat
|
||||
else
|
||||
cmd_close = "echo '"..close_min.." "..close_hour.." * * "..week_repeat.." /sbin/wifi down "..wnet..";"
|
||||
..close_min..close_hour..open_min..open_hour..week_repeat
|
||||
..">/dev/null 2>/dev/null' >> /etc/crontabs/root; crontab /etc/crontabs/root;"
|
||||
|
||||
cmd_open = "echo '"..open_min.." "..open_hour.." * * "..week_repeat.." /sbin/wifi up "..wnet..";"
|
||||
..close_min..close_hour..open_min..open_hour..week_repeat
|
||||
..">/dev/null 2>/dev/null' >> /etc/crontabs/root; echo '' >> /etc/crontabs/root; crontab /etc/crontabs/root;"
|
||||
end
|
||||
luci.http.write_json(cmd_close)
|
||||
luci.http.write_json(cmd_open)
|
||||
|
||||
exec_cmd_in_sh(cmd_close)
|
||||
posix.sleep(1)
|
||||
exec_cmd_in_sh(cmd_open)
|
||||
end
|
||||
|
||||
-----------------------------------------------stop smart wifi------------------------------------
|
||||
function set_smart_wifi_stop(update_flag)
|
||||
local cmd = "sed -i '/"..update_flag.."/d' /etc/crontabs/root; crontab /etc/crontabs/root"
|
||||
exec_cmd_in_sh(cmd)
|
||||
luci.http.write_json(cmd)
|
||||
end
|
||||
|
||||
|
||||
|
||||
--------------------------------------------update smart wifi-------------------------------------
|
||||
--(stop + set) smartwifi
|
||||
|
||||
|
||||
|
||||
|
||||
----------------------------------------------smart wifi api--------------------------------------
|
||||
--function: 定时wifi开关shell形式
|
||||
--author: rh_Jameson
|
||||
function set_smart_wifi_updown()
|
||||
|
||||
--get para
|
||||
--wnet = luci.http.formvalue("wnet")
|
||||
--close_hour = luci.http.formvalue("close_hour")
|
||||
--close_min = luci.http.formvalue("close_min")
|
||||
--open_hour = luci.http.formvalue("open_hour")
|
||||
--open_min = luci.http.formvalue("open_min")
|
||||
--repeat_var_from_http = luci.http.formvalue("repeat_var")
|
||||
--func = luci.http.formvalue("func")
|
||||
|
||||
--test
|
||||
local wnet = 'mt7628'
|
||||
local close_hour = "07"
|
||||
local close_min = "25"
|
||||
local open_hour = "07"
|
||||
local open_min = "26"
|
||||
local repeat_var_from_http = "1111111"
|
||||
local func = luci.http.formvalue("func")
|
||||
--para err manage
|
||||
if string.len(repeat_var_from_http) ~= 7 then
|
||||
luci.http.write_json("len error")
|
||||
return
|
||||
end
|
||||
|
||||
--get repeat
|
||||
local week_repeat = ""
|
||||
for i = 0, #repeat_var_from_http do
|
||||
tmp = string.sub(repeat_var_from_http, i, i)
|
||||
if tmp == "1" then
|
||||
if i == #repeat_var_from_http then
|
||||
week_repeat = week_repeat..0
|
||||
break
|
||||
end
|
||||
week_repeat = week_repeat..tostring(i)..","
|
||||
end
|
||||
end
|
||||
if week_repeat == "" then
|
||||
week_repeat = "non-repeat"
|
||||
end
|
||||
--exec
|
||||
if func == "set" then
|
||||
fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat)
|
||||
elseif func == "stop" or func == "update" then
|
||||
local update_flag = luci.http.formvalue("flag")
|
||||
set_smart_wifi_stop(update_flag)
|
||||
|
||||
if func == "update" then
|
||||
posix.sleep(1)
|
||||
fork_smart_wifi_updown(wnet, close_hour, close_min, open_hour, open_min, week_repeat)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
102
watchcat_scan_for_r13/socket_client.cpp
Normal file
102
watchcat_scan_for_r13/socket_client.cpp
Normal file
@ -0,0 +1,102 @@
|
||||
/*************************************************************************
|
||||
> File Name: socket_client.cpp
|
||||
> Description:
|
||||
> Conclusion:
|
||||
> Author: rh_Jameson
|
||||
> Created Time: 2015年05月21日 星期四 16时19分58秒
|
||||
************************************************************************/
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<unistd.h>
|
||||
#include<string.h>
|
||||
#include<sys/socket.h>
|
||||
#include<arpa/inet.h>
|
||||
#include<netinet/in.h>
|
||||
#include<sys/types.h>
|
||||
#include<netdb.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <net/if.h>
|
||||
/**
|
||||
客户端实现广播
|
||||
**/
|
||||
#define IP_FOUND "sysinfo"
|
||||
#define IP_FOUND_ACK "IP_FOUND_ACK"
|
||||
#define IFNAME "wlan2"
|
||||
#define MCAST_PORT 51232
|
||||
int main(int argc,char*argv[]){
|
||||
int ret=-1;
|
||||
int sock=-1;
|
||||
int so_broadcast=1;
|
||||
struct ifreq ifr;
|
||||
struct sockaddr_in broadcast_addr;//广播地址
|
||||
struct sockaddr_in from_addr;//服务端地址
|
||||
socklen_t from_len=sizeof(from_addr);
|
||||
int count=-1;
|
||||
fd_set readfd;//读文件描述符集合
|
||||
char buffer[1024];
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec=2;//超时时间为2秒
|
||||
timeout.tv_usec=0;
|
||||
sock=socket(AF_INET,SOCK_DGRAM,0);//建立数据报套接字
|
||||
|
||||
if(sock<0){
|
||||
printf("HandleIPFound:sock init error\n");
|
||||
return 0;
|
||||
}
|
||||
//将使用的网络接口名字复制到ifr.ifr_name中,由于不同的网卡接口的广播地址是不一样的,因此指定网卡接口
|
||||
strncpy(ifr.ifr_name,IFNAME,strlen(IFNAME));
|
||||
//发送命令,获得网络接口的广播地址
|
||||
if(ioctl(sock,SIOCGIFBRDADDR,&ifr)==-1){
|
||||
perror("ioctl error");
|
||||
return 0;
|
||||
}
|
||||
//将获得的广播地址复制到broadcast_addr
|
||||
memcpy(&broadcast_addr,&ifr.ifr_broadaddr,sizeof(struct sockaddr_in));
|
||||
//设置广播端口号
|
||||
//inet_aton("255.255.255.255", &broadcast_addr.sin_addr);
|
||||
printf("broadcast IP is:%s\n",inet_ntoa(broadcast_addr.sin_addr));
|
||||
broadcast_addr.sin_family=AF_INET;
|
||||
broadcast_addr.sin_port=htons(MCAST_PORT);
|
||||
//默认的套接字描述符sock是不支持广播,必须设置套接字描述符以支持广播
|
||||
ret=setsockopt(sock,SOL_SOCKET,SO_BROADCAST,&so_broadcast,sizeof(so_broadcast));
|
||||
//发送多次广播,看网络上是否有服务器存在
|
||||
int times=10;
|
||||
int i=0;
|
||||
for(i = 0; i < times; i++){//一共发送10次广播,每次等待2秒是否有回应
|
||||
//广播发送服务器地址请求
|
||||
timeout.tv_sec = 2;//超时时间为2秒
|
||||
timeout.tv_usec = 0;
|
||||
ret=sendto(sock,IP_FOUND, strlen(IP_FOUND), 0, (struct sockaddr*)&broadcast_addr, sizeof(broadcast_addr));
|
||||
if(ret == -1){
|
||||
continue;
|
||||
}
|
||||
|
||||
//文件描述符清0
|
||||
FD_ZERO(&readfd);
|
||||
//将套接字文件描述符加入到文件描述符集合中
|
||||
FD_SET(sock,&readfd);
|
||||
//select侦听是否有数据到来
|
||||
ret=select(sock+1,&readfd,NULL,NULL,&timeout);
|
||||
switch(ret){
|
||||
case -1:
|
||||
break;
|
||||
case 0:
|
||||
printf("timeout\n");
|
||||
break;
|
||||
default:
|
||||
//接收到数据
|
||||
if(FD_ISSET(sock,&readfd)){
|
||||
count = recvfrom(sock, buffer, 1024, 0, (struct sockaddr*)&from_addr,&from_len);//from_addr为服务器端地址
|
||||
printf("recvmsg is %s\n", buffer);
|
||||
if(strstr(buffer, IP_FOUND_ACK)){
|
||||
printf("found server IP is:%s\n",inet_ntoa(from_addr.sin_addr));
|
||||
//服务器端的发送端口号
|
||||
printf("Server Port:%d\n",htons(from_addr.sin_port));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
91
watchcat_scan_for_r13/watchcat.c
Normal file
91
watchcat_scan_for_r13/watchcat.c
Normal file
@ -0,0 +1,91 @@
|
||||
/*
|
||||
* "
|
||||
* UDP BS INFO Server
|
||||
*/
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static char* pcmdurl = "wget http://127.0.0.1/cgi-bin/luci/bs/info -q -O -";
|
||||
int read_sysinfo(char* filebuf, int filebuf_len)
|
||||
{
|
||||
FILE * fstr = NULL;
|
||||
int ret;
|
||||
int haveread = 0;
|
||||
fstr = popen(pcmdurl, "rb");
|
||||
if (fstr == NULL) {
|
||||
printf("popen fail");
|
||||
return 0;
|
||||
}
|
||||
while (filebuf_len > 0) {
|
||||
ret = fread(filebuf + haveread, 1, filebuf_len, fstr);
|
||||
if(ret <= 0)
|
||||
break;
|
||||
haveread += ret;
|
||||
filebuf_len -= ret;
|
||||
}
|
||||
printf("READ:******\n%s", filebuf);
|
||||
pclose(fstr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc ,char * argv[])
|
||||
{
|
||||
int sock_fd;
|
||||
socklen_t addr_len;
|
||||
struct sockaddr_in addr;
|
||||
struct sockaddr_in client_addr ;
|
||||
unsigned short port = 51232;
|
||||
char buf[1024];
|
||||
int len ;
|
||||
if(argc > 1) {
|
||||
port = (unsigned short)atoi(argv[1]);
|
||||
}
|
||||
sock_fd = socket(PF_INET,SOCK_DGRAM,0);
|
||||
int on = 1;
|
||||
setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
|
||||
if (sock_fd == -1) {
|
||||
perror("socket");
|
||||
return -1;
|
||||
}
|
||||
memset(&addr,0,sizeof(addr));
|
||||
addr.sin_port = htons(port);
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
if (bind(sock_fd,(const struct sockaddr *)&addr,sizeof(addr)) == -1) {
|
||||
perror("bind");
|
||||
close(sock_fd);
|
||||
return -2;
|
||||
}
|
||||
printf("UDP Echo Server listen on %d\n", port);
|
||||
while(1) {
|
||||
int filebuf_len = 2048;
|
||||
char *filebuf = (char *)calloc(filebuf_len, sizeof(char));
|
||||
if (filebuf == NULL) {
|
||||
perror("molloc fail");
|
||||
continue;
|
||||
}
|
||||
memset(&client_addr,0,sizeof(client_addr));
|
||||
memset(buf, 0x00, sizeof(buf));
|
||||
client_addr.sin_family = AF_INET;
|
||||
addr_len = sizeof(client_addr);
|
||||
len = recvfrom(sock_fd, buf, sizeof(buf), 0, (struct sockaddr *)&client_addr, &addr_len);
|
||||
if ((len <=0) || (strncmp(buf, "sysinfo", sizeof("sysinfo")-1) != 0)) {
|
||||
sleep(1);
|
||||
continue;
|
||||
}
|
||||
buf[len] = 0;
|
||||
printf("client addr %s ,port %d,buf %s\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port),buf);
|
||||
read_sysinfo(filebuf, filebuf_len);
|
||||
sendto(sock_fd,filebuf,strlen(filebuf)+1,0,(struct sockaddr *)&client_addr,addr_len);
|
||||
free(filebuf);
|
||||
}
|
||||
close(sock_fd);
|
||||
|
||||
return 0;
|
||||
}
|
135
watchcat_scan_for_r13/watchcat_print_flag.c
Normal file
135
watchcat_scan_for_r13/watchcat_print_flag.c
Normal file
@ -0,0 +1,135 @@
|
||||
/*
|
||||
* UDP BS INFO Server
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
char* pcmdurl = "wget http://127.0.0.1/cgi-bin/luci/bs/info -q -O -";
|
||||
|
||||
//char filebuf[2048];
|
||||
|
||||
int PipeReadBsInfo(char* filebuf)
|
||||
{
|
||||
printf("pipe test\n");
|
||||
FILE * fstr = NULL;
|
||||
int ret;
|
||||
int haveread = 0;
|
||||
fstr = popen(pcmdurl, "rb");
|
||||
|
||||
if(fstr == NULL){
|
||||
printf("popen fail");
|
||||
return 0;
|
||||
}
|
||||
while(1){
|
||||
ret = fread(filebuf + haveread, 1, 1024, fstr);
|
||||
if(ret <= 0)
|
||||
break;
|
||||
haveread += ret;
|
||||
}
|
||||
printf("READ:******%d chars\n", haveread);
|
||||
printf("READ:%s\n", filebuf);
|
||||
pclose(fstr);
|
||||
}
|
||||
|
||||
int main(int argc ,char * argv[])
|
||||
{
|
||||
unsigned short port = 51232;
|
||||
int sock_fd;
|
||||
struct sockaddr_in addr;
|
||||
|
||||
struct sockaddr_in client_addr ;
|
||||
socklen_t addr_len;
|
||||
|
||||
char buf[1024];
|
||||
int len ;
|
||||
perror("init test1\n");
|
||||
printf("init test2\n");
|
||||
if(argc >1)
|
||||
{
|
||||
port = (unsigned short)atoi(argv[1]);
|
||||
}
|
||||
|
||||
sock_fd = socket(PF_INET,SOCK_DGRAM,0);
|
||||
//set broadcast
|
||||
int on = 1;
|
||||
setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
|
||||
|
||||
if(sock_fd == -1)
|
||||
{
|
||||
perror("socket");
|
||||
return -1;
|
||||
}
|
||||
memset(&addr,0,sizeof(addr));
|
||||
addr.sin_port = htons(port);
|
||||
addr.sin_family = AF_INET;
|
||||
//addr.sin_addr.s_addr = inet_addr("0.0.0.0");
|
||||
//INADDR_ANY = 0 = inet_addr("0.0.0.0")
|
||||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
|
||||
if(bind(sock_fd,(const struct sockaddr *)&addr,sizeof(addr)) == -1)
|
||||
{
|
||||
perror("bind");
|
||||
close(sock_fd);
|
||||
return -2;
|
||||
}
|
||||
//fprintf(stdout,"UDP Echo Server listen on %d\n",port);
|
||||
printf("init test33\n");
|
||||
printf("UDP Echo Server listen on %d\n", port);
|
||||
printf("init test34\n");
|
||||
while(1)
|
||||
{
|
||||
//code change
|
||||
char *filebuf = (char *)malloc(2048 * sizeof(char));
|
||||
if(filebuf == NULL)
|
||||
{
|
||||
//printf("molloc fail");
|
||||
perror("molloc fail");
|
||||
return 0;
|
||||
}
|
||||
|
||||
memset(filebuf, 0x00, sizeof(filebuf));
|
||||
memset(&client_addr,0,sizeof(client_addr));
|
||||
memset(buf, 0x00, sizeof(buf));
|
||||
|
||||
client_addr.sin_family = AF_INET;
|
||||
addr_len = sizeof(client_addr);
|
||||
//len = recvfrom(sock_fd,buf,sizeof(buf),0,NULL,NULL);
|
||||
|
||||
printf("while test1\n");
|
||||
len = recvfrom(sock_fd,buf,sizeof(buf),0,(struct sockaddr *)&client_addr,&addr_len);
|
||||
printf("recv test\n");
|
||||
|
||||
//if(len <= 0){
|
||||
printf("while test1 %s\n", buf);
|
||||
if((len <=0) || (strncmp(buf, "sysinfo", sizeof("sysinfo")-1) != 0)){
|
||||
//sleep(1);
|
||||
printf("%d\n", len);
|
||||
printf("%d\n", (strncmp(buf, "sysinfo", sizeof("sysinfo")-1)));
|
||||
continue;
|
||||
}
|
||||
printf("recvfrom test2\n");
|
||||
buf[len] = 0;
|
||||
printf("client addr %s ,port %d,buf %s\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port),buf);
|
||||
//ReadBsInfo();
|
||||
/*
|
||||
*if(strlen(filebuf) == 0){
|
||||
* PipeReadBsInfo();
|
||||
*}
|
||||
*/
|
||||
printf("send begin test\n");
|
||||
PipeReadBsInfo(filebuf);
|
||||
printf("%s %d : %s\n", __func__, __LINE__, filebuf);
|
||||
sendto(sock_fd,filebuf,strlen(filebuf)+1,0,(struct sockaddr *)&client_addr,addr_len);
|
||||
printf("send over test\n");
|
||||
free(filebuf);
|
||||
}
|
||||
close(sock_fd);
|
||||
printf("close test\n");
|
||||
}
|
Loading…
Reference in New Issue
Block a user