一个openwrt无线踢人的脚本

一个openwrt无线踢人的脚本, 模拟ubnt的踢人,原理根据:iw dev wlan0 station del mac ,本想用openwrt里的sh脚本命令实现,但原来openwrt的sh的脚本命令是ash来的,实现起来麻烦,于是用lua脚本实现,操作非常方便.

废话不说,以下是脚本:

function split(s, delim)
    if type(delim) ~= "string" or string.len(delim) <= 0 then
        return
    end

    local start = 1
    local t = {}
    while true do
    local pos = string.find (s, delim, start, true) -- plain find
        if not pos then
          break
        end

        table.insert (t, string.sub (s, start, pos - 1))
        start = pos + string.len (delim)
    end
    table.insert (t, string.sub (s, start))

    return t
end


local find = string.find
local sub = string.sub
function trim8(s)
  local i1,i2 = find(s,'^%s*')
  if i2 >= i1 then s = sub(s,i2+1) end
  local i1,i2 = find(s,'%s*$')
  if i2 >= i1 then s = sub(s,1,i1-1) end
  return s
end



local ming = io.popen('iw dev | grep Interface | cut -f 2 -s -d" "')
local getstr = ming:read("*all")


local gg=""

aaa=split(getstr,"\n")
local shu=#aaa-1

for i=1,#aaa-1 do
  cc=aaa[i]
   gg=gg.."iw dev "..cc.." station dump;"  
end

local mingling=gg



local t = io.popen(mingling)
local a = t:read("*all")
--print(a)

--astring= "----分割?--"
--print(astring)


aa=split(a,"TDLS peer")
for i=1,#aa-1 do
   bb=aa[i]
   cc = split(bb,"(on")
   dd = cc[1]
   ee = split(dd, "Station")
   ff = trim8(ee[2])
   --print(ff)   
   
   ii=find(bb, "[-",1,true)
   if ii ~=nil then
     cc2 = split(bb," [-")
   else
     cc2 = split(bb," dBm")
   end
   dd2 = split(cc2[1],"signal: ")
   ee2 = trim8(dd2[2])
   --print(ee2)
   ee3 = tonumber(ee2)


   ccc = split(bb, "wlan")
   ddd = split(ccc[2],")")
   eee  = "wlan"..ddd[1]
   --print(eee)

   if ee3 < -81 then
      datex=os.date("%Y-%m-%d %H:%M:%S");
      print("ti ren: "..datex)    
      os.execute("iw dev "..eee.." station del "..ff)
      print("iw dev "..eee.." Station del "..ff)

   end
   
   --print("----")
end

做成脚本,再定时任务哪里设置,每5分钟执行一次, 在任务里填写:
*/5 * * * * /usr/bin/lua /root/tiren.lua >>/tmp/dellist.txt &

现在是小于-81db就删掉,这样无线连接效果好很多。
另外想说的就是lua脚本操作起来还是很方便,容易上手呢。

其实也想根据这个原理在tomato哪里尝试,但找不到相关的命令,手上还是有tomato的路由。

关于无聊人

一个无聊人而已
此条目发表在技术生活分类目录。将固定链接加入收藏夹。

一个openwrt无线踢人的脚本》有4条回应

  1. James说:

    楼主,这个脚本怎么执行,多谢。

  2. 无聊人说:

    放在openwrt的定时任务里,用lua执行这剧本

  3. king说:

    博主,这个脚本可否再优化下?我使用的情况:网络是断开了,但是信号还是一直连着!检测信号可否按秒来踢终端?

  4. 无聊人说:

    信号是连着的,这个确实,这个是del mac命令执行后的效果,没办法踢到它自动断线的,至于按秒来说,自己改哪个计划任务的命令,但每秒都执行,弱弱的路由器cpu占用会很高的,估计可能令cpu受不了,后果严重。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注