多ADSL动态IP策略路由脚本(附详解)

 

以下脚本是根据louyc 的ROS下多ADSL and DDNSupdate动态域名脚本改编的,已经测试通过,大家使用时不需要做任何修改,只要确保各ADSL以及网卡名不要相同就可以直接使用,使用过程中增加新ADSL和禁止ADSL开放ADSL都能自动更新,就是删除ADSL时需要手工删除相应IP及路由。因本人网络技术很菜,脚本写的不好,不要扔鸡蛋,俺再修改,对负载均衡的实现比较陌生,希望各位指点一下(给点参考资料也可),如果做的好,将脚本整合一起发上来。
/system scheduler add name=ADSLManage disabled=no interval=5s on-event={
    /interface pppoe-client
    :foreach pppoeClient in[find disable=no] do={
        :local pppoeClientName [get $pppoeClient name]
        :local pppoeInterface [get $pppoeClient interface]
        :local interfaceComment [/interface get [/interface find name=$pppoeInterface] comment]
        monitor $pppoeClientName once do={
            :local findIndex [:find $interfaceComment "-"]
            :local oldStatus ""
            :local oldIP ""
            :local ipAddress ""
            :local newIP ""
            :if (findIndex!=nothing) do={
                :set oldStatus [:pick $interfaceComment 0 $findIndex]
                :set oldIP [:pick $interfaceComment ($findIndex+1) [:len $interfaceComment]]
            } else={
                :set oldStatus $interfaceComment
            }
            :if ($status="connected") do={
                :set ipAddress [/ip address get [/ip address find interface=$pppoeClientName dynamic=yes] address]
                :set newIP [:pick $ipAddress 0 [:find $ipAddress "/"]]
            }
            :if ($status!=$oldStatus||($status="connected"&&$oldStatus="connected"&&$oldIP!=$newIP)) do={
                :if ($oldStatus="connected") do={
                    /ip address remove ($pppoeClientName . "_IP_Address")
                    /ip route remove ($pppoeClientName . "_Route")
                }
                :if ($status="connected") do={
                    /ip address add comment=($pppoeClientName . "_IP_Address") address=$ipAddress network=$newIP broadcast=$newIP interface=$pppoeClientName disabled=no
                    /ip route add comment=($pppoeClientName . "_Route") gateway=$newIP routing-mark=($pppoeClientName . "_Routing_Mark")
                    /interface set [/interface find name=$pppoeInterface] comment=($status . "-" . $newIP)
                }
                :if ($status="disconnected"||$status="dialing...") do={
                    /interface set [/interface find name=$pppoeInterface] comment=$status
                }
            }
        }
    }
    /interface pppoe-client
    :foreach pppoeClient in[find disable=yes] do={
        :local pppoeClientName [get $pppoeClient name]
        :local pppoeInterface [get $pppoeClient interface]
        :local interfaceComment [/interface get [/interface find name=$pppoeInterface] comment]
        :if ($interfaceComment!="")
        {
            :if ([:find $interfaceComment "connected"]!=nothing) do={
                /ip address remove ($pppoeClientName . "_IP_Address")
                /ip route remove ($pppoeClientName . "_Route")
            }
            /interface set [/interface find name=$pppoeInterface] comment=""
        }
    }
}

#建立每5秒钟执行一次的任务
/system scheduler add name=ADSLManage disabled=no interval=5s on-event={
    /interface pppoe-client
    #对开放的pppoe-client循环一遍
    :foreach pppoeClient in[find disable=no] do={
        #取pppoe-client的name
        :local pppoeClientName [get $pppoeClient name]
        #取pppoe-client的interface
        :local pppoeInterface [get $pppoeClient interface]
        #取pppoe-client的interface的备注
        :local interfaceComment [/interface get [/interface find name=$pppoeInterface] comment]
        #监视pppoe-client的状态
        monitor $pppoeClientName once do={
            #说明:adsl的拨号状态及IP地址用“-”分隔,放入pppoe-client的interface的备注,作为状态标记
            #取“-”的位置
            :local findIndex [:find $interfaceComment "-"]
            #旧状态定义
            :local oldStatus ""
            #旧IP定义
            :local oldIP ""
            #ADSL拨号的IP地址定义
            :local ipAddress ""
            #ADSL的IP地址定义
            :local newIP ""
            #如果找到“-”的位置
            :if (findIndex!=nothing) do={
                #取旧状态
                :set oldStatus [:pick $interfaceComment 0 $findIndex]
                #取旧IP地址
                :set oldIP [:pick $interfaceComment ($findIndex+1) [:len $interfaceComment]]
            } else={
                #设置旧状态
                :set oldStatus $interfaceComment
            }
            #如果拨号成功
            :if ($status="connected") do={
                #取ADSL拨号的IP地址
                :set ipAddress [/ip address get [/ip address find interface=$pppoeClientName dynamic=yes] address]
                #取ADSL的IP地址
                :set newIP [:pick $ipAddress 0 [:find $ipAddress "/"]]
            }
            #如果(监控的状态不等于旧状态)或者(监控的状态为已连接并且旧状态为已连接并且新IP不等于旧IP)
            :if ($status!=$oldStatus||($status="connected"&&$oldStatus="connected"&&$oldIP!=$newIP)) do={
                #如果旧状态为已连接
                :if ($oldStatus="connected") do={
                    #删除ADSL地址
                    /ip address remove ($pppoeClientName . "_IP_Address")
                    #删除路由地址
                    /ip route remove ($pppoeClientName . "_Route")
                }
                #如果监控状态为已连接
                :if ($status="connected") do={
                    #增加ADSL地址
                    /ip address add comment=($pppoeClientName . "_IP_Address") address=$ipAddress network=$newIP broadcast=$newIP interface=$pppoeClientName disabled=no
                    #增加ADSL路由地址
                    /ip route add comment=($pppoeClientName . "_Route") gateway=$newIP routing-mark=($pppoeClientName . "_Routing_Mark")
                    #设置pppoe-client的interface的备注为已连接+“-”+新IP地址,不能设置pppoe-client的备注(改变pppoe-client的备注时,会自动重拨)
                    /interface set [/interface find name=$pppoeInterface] comment=($status . "-" . $newIP)
                }
                #如果监控状态为离线或正拨号
                :if ($status="disconnected"||$status="dialing...") do={
                    #设置pppoe-client的interface的备注为离线或正拨号
                    /interface set [/interface find name=$pppoeInterface] comment=$status
                }
            }
        }
    }
    /interface pppoe-client
    #对禁止的pppoe-client循环一遍
    :foreach pppoeClient in[find disable=yes] do={
        #取pppoe-client的name
        :local pppoeClientName [get $pppoeClient name]
        #取pppoe-client的interface
        :local pppoeInterface [get $pppoeClient interface]
        #取pppoe-client的interface的备注
        :local interfaceComment [/interface get [/interface find name=$pppoeInterface] comment]
        #如果pppoe-client的interface的备注不等于空字符串
        :if ($interfaceComment!="")
        {
            #pppoe-client的interface的备注中的状态为已连接
            :if ([:find $interfaceComment "connected"]!=nothing) do={
                #删除ADSL地址
                /ip address remove ($pppoeClientName . "_IP_Address")
                #删除路由地址
                /ip route remove ($pppoeClientName . "_Route")
            }
            #设置pppoe-client的interface的备注为空字符串
            /interface set [/interface find name=$pppoeInterface] comment=""
        }
    }
}

你可能感兴趣的:(网络,职场,路由,休闲)