Page 1 of 1

RouterOS Scripting Problem

Posted: Sat Jan 12, 2013 1:15 pm
by georgechyo
Hello,
I have a trouble in simple script:
local peer [ip ipsec remote-peers get value-name=remote-address number=0]
:log info $peer
It should get ip address of remote peer and make a log, when i run it from the winbox or schedule, it makes a blank log, but when i run the same script from the CLI it logs well.
I noticed thet when i reload router and run script from winbox it works well, but when i flush ipsec peers and reconnect, the script does the same.

Re: RouterOS Scripting Problem

Posted: Sat Jan 12, 2013 1:43 pm
by rdc
what version are you running this script?

Is the local part supposed to have : (:local)?

Re: RouterOS Scripting Problem

Posted: Sat Jan 12, 2013 10:38 pm
by tomaskir
Works for me like this.
:local peer [/ip ipsec remote-peers get 0 remote-address]
:log info "Peer 0 - $peer"

Re: RouterOS Scripting Problem

Posted: Sat Jan 12, 2013 11:15 pm
by georgechyo
rdc
I tried different versions, tried with :local, but result is the same.

tomaskir
doesn't worked for me :(
Result is: "Peer 0 - " when run from winbox or schedule, but works when run from CLI.
when i reload router it works, but when i flush peers and reestablish it still does the same :(
I don't understand, is it a bug or what?

Re: RouterOS Scripting Problem

Posted: Sun Jan 13, 2013 12:47 am
by tomaskir
Actually, it has to do with using 0 as the identifier.
This should work always, and will return all pears as an array $peer. I use a simply :foreach to write them out.
:local peer [/ip ipsec remote-peers get [/ip ipsec remote-peers find] remote-address]

:foreach i in $peer do={
  :log info "Peer - $i"
}

Re: RouterOS Scripting Problem

Posted: Sun Jan 13, 2013 12:06 pm
by georgechyo
tomaskir

Thanks for your help, but it worked when there was a single tunnel (remote peer), but with more than one peer, it generates error: "invalid internal item number", in both winbox and command line.
I think that command
[/ip ipsec remote-peers get [/ip ipsec remote-peers find ] remote-address]
doesn't return an array value.
I'm week at scripting, but i need to check which tunnels are connected :(

Re: RouterOS Scripting Problem

Posted: Sun Jan 13, 2013 2:31 pm
by tomaskir
I tested with one peer only yes. I fixed it to work with multiple peers like this:
:local idArray [/ip ipsec remote-peers find]

:foreach i in $idArray do={
  :log info [/ip ipsec remote-peers get $i remote-address]
}

Re: RouterOS Scripting Problem

Posted: Sun Jan 13, 2013 9:52 pm
by georgechyo
tomaskir

It works :) Thank you very much :)