RouterOS Scripting Problem

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.

what version are you running this script?

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

Works for me like this.

:local peer [/ip ipsec remote-peers get 0 remote-address]
:log info "Peer 0 - $peer"

rdc
I tried different versions, tried with :local, but result is the same.

tomaskir
doesn’t worked for me :frowning:
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 :frowning:
I don’t understand, is it a bug or what?

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"
}

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 :frowning:

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]
}

tomaskir

It works :slight_smile: Thank you very much :slight_smile: