在Mikrotik脚本中处理错误

我在RB493G上安装了RouterOS 5.14。 我需要写脚本,启动

/tool fetch ... 

执行提取可能会导致错误,这是正常的(URL可能有时不可用)。 脚本错误挂起。 有什么办法可以忽略它吗?


解:

 [admin@Mikrotik] >> /system script 0 name=safe-fetch source= :global done :global url /tool fetch $url :Set done=true 1 name=test source= :global done :global url="google.com" :set done false :execute safe-fetch :local counter 0 :while ( $done != true && $counter < 10 ) do={ :set counter ($counter+1) :delay 0.2 } if ($done = "true") do={ :put "Fetch OK" } else={ :put "Fetch ERROR" } 

警告:没有logging“:执行”被使用。

看来这种脚本没有error handling 。 你应该采用这个人提出的解决scheme:

所以你必须有两个脚本,一个运行,做一些事情,处理情况,当一些价值没有收到预期的,其他脚本做的工作,可以失败。 因此,如果第二个脚本失败了,那么第一个调用第二个脚本来完成这个工作的将继续。

更新自RouterOS v6.2

你现在可以用一个error handling程序创build一个do块:

 :do { /tool fetch $url :put "Fetch OK" } on-error={ :put "Fetch ERROR"};