Find item by ID

Hi, it is possible to find items by .id? I’m unable to get any results.

Example:
[admin@FireHouse] /ip ipsec peer> print detail where .id=1
Flags: X - disabled, D - dynamic, R - responder

but there is a peer with number 1. or how you target specific rules/peer/policy etc with script?
I need to check “if the peer is enabled” in the script but I’m unable to find any way to check this.

Thanks. I can’t find any solution in search or google.

The numbers you see in “print” output are NOT the same as the ID!
When you do a print command, the router constructs an internal temporary table of all numbers and the corresponding ID, then when you do something which references a number it translates that back to the ID.
But this works only in an interactive session, not in a script.
When you need to reference some specific item in a find command, you need to match on some other value of that item.
A commonly used trick is to set a comment on the item and then search for the comment. Of course this can only be done for static items.
In that case you could also change the name and search for that name.

Thanks so my best option is search by comment. Not the most elegant way how to do it but it probably will works.

In signature you see a link and that topic also contains a script to restart a peer if it went down. It only restarts peers that are enabled and you can rewrite that to enable disabled peers.

A meaningful name for the peer is essential or you have to use the comment field as already suggested.

This is the little script: http://forum.mikrotik.com/t/keep-ikev2-connections-running-script/137419/4

Underneath the latest version that can can do more:

# IKEv2 maintainer by Blacklister version: 20200510 2.0
# Not for commercial use
# Restarts the IKEv2 connection when it stops and is enabled in /ip ipsec peers
# That is restart is tried 15 times and then it stops for 15 times and so on.
# The Interval set on this scheduler spaces that out in time
{
# only load myFunc if not already loaded
if [/system script environment find name=myFuncPresent] do={} else={/system script run myFunc;}

#Change to IPsec peer to have that as default location to run commands
/ip ipsec peer

#Loop through names of the peers and see if they need restart
:foreach peerID in=[/ip ipsec peer find disabled=no] do={ 
   :set $peerName [/ip ipsec peer get $peerID name ];
      :if ([/ip ipsec policy find peer=$peerName]="") do={
          :set $cleanedPeerName [$replaceCharacterFunc $peerName "-"]
          :local envName ("restarted".$cleanedPeerName) ;
          :if ([/sys script env find name=$envName]) do={} else={[:parse "global $envName 0"];}
          :local tempCount [/sys script env get [find name="$envName"] value ]
          :set $tempCount ($tempCount + 1)
           # if the counter comes over 15 then a time-out of 15 repeats, of the used scheduled interval is activated.
           :if ($tempCount < 16) do={
              :log warning "$peerName not active, restarting.." 
              disable $peerName; :delay 1500ms; enable $peerName}
           :if ($tempCount > 30) do={:set $tempCount 1; :log warning "Counter reset to 1 for $envName"}
          [:parse "global $envName $tempCount"]
  }   } }; # if..if..foreach

# hier nog het wissen van de variable en de counter als de verbinding weer staat.
# You can create a schedule that removes the restarted variables with the following script 
# /system script environment remove [find name~"restarted"];
}

This line looks if a peer is enabled and you can reverse that by using disabled=yes.

:foreach peerID in=[/ip ipsec peer find disabled=yes] do={

This is the link to the Function that is called in this script. You can can copy the top one to this script to run it locally or two lower ones to run them system-wide.

http://forum.mikrotik.com/t/cleaning-characters-from-string-for-use-in-variablename/139391/1