Lua 定时监控网关是否可达

--version 1.0.0

require("utils")

IpList = { "192.168.134.38"}  -- 格式: IpList = {"185.101.1.129","192.168.134.128"}
LogPath = "log.txt"  --日志路径和名称
WinPingCmd = "C:\\Windows\\System32\\PING.EXE -w 2000 -n 3 "   -- ping 命令   -w 单次ping超时时间,单位毫秒  -n ping次数
IntervalTime = 3000 -- 轮询间隔 单位 毫秒

local function delRouteByGetway(ip)
    local handler = io.popen("route print -4")
    local response = handler:read("*a")
    local routeIndex = string.find(response,"永久路由")

    local permanentRoute =  string.sub(response,routeIndex,string.len(response))

    if(string.find(permanentRoute,"无", 1)) then
        return
    end

    local list = utils.split(permanentRoute,"[^\r\n]+")
    for index, ipStr in ipairs(list) do
        local ipArr = utils.split(ipStr,"[^ ]+")
      
        if(ipArr[3]==ip) then
           print("Match successful")
           local result = utils.delRoute(ipArr[1])
           utils.log("del route:"..ipArr[1].." getway"..ip..result)
        end
    end

   
    print(list)
     
    print(permanentRoute)
end

utils.log("ChangeRoute.exe start ........")

RemoveIpList = {}

while true do
    IsAllPingFail = true
    for i,ip in ipairs(IpList)
    do
        if(not utils.isInclude(ip,RemoveIpList)) then
            local handler = io.popen(WinPingCmd..ip)
            local response = handler:read("*a")
            print(response)
            handler.close()
            if(string.find(response,"TTL=", 1)) then
                IsAllPingFail = false
            else    
                utils.log("ip:"..ip.." ping fail")
                delRouteByGetway(ip)
   
                table.insert(RemoveIpList, ip)
            end
        end
   
    end

    if(IsAllPingFail) then
        utils.log("All Ping Fail ........")
        utils.log("The program is about to exit........")
        io.popen("stop.bat")
        break;
    end
 
    print("start sleep")
    utils.sleep(IntervalTime)
    print("end sleep")
end

utils.log("ChangeRoute.exe stop ........")

实现功能  定时轮询网关是否能ping通,不同就删除

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值