找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 5655|回复: 12

[脚本] ROS脚本运行久了,变量的值会掉失。

[复制链接]
发表于 2008-3-11 16:54:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

×
:local bl2ip [/ip address get [/ip address find dynamic=yes interface=wan.pppoe.tel2] address]
:local bl2ip [:pick $bl2ip 0 [:find $bl2ip "/"]]
:local bl2lastip [/ip route get [/ip route find routing-mark=2] gateway]

:if (($bl2ip) != (0.0.0.0)) do={
:if ($bl2ip != $bl2lastip) do={

/ip address set [/ip address find comment=BothLine.wan2] address=$bl2ip network=$bl2ip broadcast=$bl2ip
/ip fir nat set [/ip fir nat find connection-mark=2] to-addresses=$bl2ip
/ip route set [/ip route find routing-mark=2] gateway $bl2ip
/ip dns set primary-dns=202.103.96.68 secondary-dns=202.175.3.3
:log info ("BothLing2 Updata:" . $bl2ip)
}
}

我的脚本语言原意是,监测interface=wan.pppoe.tel2这个端口的IP地址变更,但当时间运行长了,发现$bl2ip这个变量的值会变成空,什么都没有。于是我想在做一句判断空值的语句,但不知道怎样写,请高手指教。
routeros
发表于 2008-3-11 19:24:28 | 显示全部楼层
先给变量赋空值,再用表达式给变量赋值,然后检查变量是不是空
routeros
回复

使用道具 举报

发表于 2008-3-11 22:55:51 | 显示全部楼层
用:len返回变量长度,如果等于0就说明空;或者判断与[:nothing]的关系,成立为空。
:if ($bl2ip=[:nothing]) do={:put 1} else={:put 0}
:put [:len $bl2ip]
提供个思路,手头没ros,没测试过代码~~
routeros
回复

使用道具 举报

 楼主| 发表于 2008-3-12 11:16:50 | 显示全部楼层
routeros
回复

使用道具 举报

 楼主| 发表于 2008-3-12 11:35:19 | 显示全部楼层
发现如果一个变量值为空的时候,根本无法作比较的。

例如:
:local bl2ip
:if ($bl2ip!=[:nothing]) do={:put a} else={:put b}

这句判断语句根本不会执行的。
routeros
回复

使用道具 举报

发表于 2008-3-12 12:21:20 | 显示全部楼层
原帖由 yescon 于 2008-3-12 11:35 发表
发现如果一个变量值为空的时候,根本无法作比较的。

例如:
:local bl2ip
:if ($bl2ip!=[:nothing]) do={:put a} else={:put b}

这句判断语句根本不会执行的。


“:local bl2ip” 后面还需要有个值的,只这样的话变量不会声明成功的;就是说如果不跟着赋值的话,变量在系统中是不存在的。可以用":env pri"命令来验证一下。
另外,local声明的是过程变量,在另外一个脚本或控制台会话是看不到的。所以不要再脚本中执行,然后到控制台查看,那样是看不到的。
routeros
回复

使用道具 举报

发表于 2008-3-12 12:22:39 | 显示全部楼层
原帖由 yescon 于 2008-3-12 11:16 发表



请问怎样赋空值呢?

:local bl2ip

是这样吗?


赋空值是这样
:local bl2ip [:nothing]
routeros
回复

使用道具 举报

发表于 2008-3-12 15:07:02 | 显示全部楼层
:set  bl2ip ""
:set  bl2ip  [/ip address get *1 address]
:if($bl2ip!="") do={:log info $bl2ip} else={:log error "not fond bl2ip"}

[ 本帖最后由 hcb 于 2008-3-12 15:08 编辑 ]
routeros
回复

使用道具 举报

 楼主| 发表于 2008-3-13 09:59:25 | 显示全部楼层
[admin@D1.2] > :local bl2ip ""
[admin@D1.2] > :if ($bl2ip!=[:nothing]) do={:put a} else={:put b}
a
以上的不是赋空值

[admin@D1.2] > :local bl2ip [:nothing]
[admin@D1.2] > :if ($bl2ip!=[:nothing]) do={:put a} else={:put b}
b
[admin@D1.2] >
这句才是真正的赋空值。

但为什么我这样的话,就不能作比较呢?
[admin@D1.2] > :local bl2ip
[admin@D1.2] > :put $bl2ip

[admin@D1.2] > :if ($bl2ip!=[:nothing]) do={:put a} else={:put b}
[admin@D1.2] >
从显示:put $bl2ip出来,它的值也是空的啊。为什么不能比较呢?
routeros
回复

使用道具 举报

发表于 2008-3-13 17:56:35 | 显示全部楼层
是我没有说明白,[:nothing]不等于空字串"",不等于0。
[admin@D1.2] > :local bl2ip ""
[admin@D1.2] > :if ($bl2ip!=[:nothing]) do={:put a} else={:put b}
这样你就能理解为什么这段代码不能正确执行了。

至于这段代码:
[admin@D1.2] > :local bl2ip
[admin@D1.2] > :put $bl2ip
[admin@D1.2] > :if ($bl2ip!=[:nothing]) do={:put a} else={:put b}
[admin@D1.2] >
我已经说过了,:local 和 :set 命令,除了要跟上变量名外,还要跟上值,否则变量不会在系统中存在。:put 和:if 如何处理不存在的变量呢?我也不知道,呵呵。
[wolf@wolf_ros2927] > :env pri
Global Variables
password=×
username=×
domainzone=×
domainname=×
Local Variables
[wolf@wolf_ros2927] > :local temp
[wolf@wolf_ros2927] > :env pri
Global Variables
password=×
username=×
domainzone=×
domainname=×
Local Variables
[wolf@wolf_ros2927] > :local temp ""
[wolf@wolf_ros2927] > :env pri
Global Variables
password=×
username=×
domainzone=×
domainname=×
Local Variables
temp=
[wolf@wolf_ros2927] >
:environment print 的作用是显示所有变量及其值。
如果你实在搞不明白这个,就用:len判断吧,返回变量字串长度。变量不存在的,等于空值的,等于:nothing的,一律返回0。
routeros
回复

使用道具 举报

发表于 2008-3-13 17:59:32 | 显示全部楼层
hcb的思路也不错,就是先给变量赋上一个初值,然后再让表达式对该变量赋值,之后检查该变量与初值的变化。应该也是可以的。
routeros
回复

使用道具 举报

发表于 2008-3-13 19:40:03 | 显示全部楼层
为什么要赋上一个初值 直接用表达式赋值 不是可以么
routeros
回复

使用道具 举报

 楼主| 发表于 2008-3-18 15:58:04 | 显示全部楼层
我现在这样解决了。谢谢各位的指教。


:local bl2ip [:nothing]
:local bl2ip [/ip address get [/ip address find dynamic=yes interface=wan.pppoe.tel2] address]
:local bl2ip [:pick $bl2ip 0 [:find $bl2ip "/"]]
:local bl2lastip [/ip route get [/ip route find routing-mark=2] gateway]


:if ($bl2ip != [:nothing]) do={
   :if (($bl2ip) != (0.0.0.0)) do={
      :if ($bl2ip != $bl2lastip) do={

      :log info ("BL2ip <> BL2lastip")
      :log info ("bl2ip: " . $bl2ip)
      :log info ("bl2lastip: " . $bl2lastip)

       /ip address set [/ip address find comment=BothLine.wan2] address=$bl2ip network=$bl2ip broadcast=$bl2ip
       /ip fir nat set [/ip fir nat find connection-mark=2] to-addresses=$bl2ip
       /ip route set [/ip route find routing-mark=2] gateway $bl2ip
       /ip dns set primary-dns=202.103.96.68 secondary-dns=202.175.3.3
       :log info ("BothLing-2 Updata:" . $bl2ip)
       }
   }

}
routeros
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|手机版|小黑屋|软路由 ( 渝ICP备15001194号-1|渝公网安备 50011602500124号 )

GMT+8, 2024-5-29 12:12 , Processed in 0.104688 second(s), 4 queries , Gzip On, Redis On.

Powered by Discuz! X3.5 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表