Black list for failed login to IPSec VPN

Error found.
Here is some data from the log:

64.62.197.52 phase1 negotiation failed.
64.62.197.217 phase1 negotiation failed.
65.49.20.109 phase1 negotiation failed.
phase1 negotiation failed due to time up 92.200.200.100[500]<=>27.115.124.10[48536] 0011223344556677:5c5c77ed781bf459
phase1 negotiation failed due to time up 92.200.200.100[500]<=>27.115.124.9[64696] 0011223344556677:53c620c089b6028e
65.49.20.124 phase1 negotiation failed.
94.206.221.159 phase1 negotiation failed.
94.206.221.159 phase1 negotiation failed.

Lines with “phase1 negotiation failed” is not equal and some lines do not contain IP at start.
So solution is to add a dot at the end of search term to ignore those lines

negotiation failed.

Script updated and based on rextendeds version. Simpler with just one loop.
Also added test for access list name to see if IP is in IPSEC access list. (it may be in multiple lists)

# Created Jotne && rextended 2022 v1.4
#
# This script add ip of user who with "IPSEC negotiation failed", "SPI* not registered" and "Invalid exchange" to a block list for 7 days
# Schedule the script to run every 5 min
# It should run on all routerOS version
# 1.3 added "Invalid exchange"
# 1.4 added dot behind "negotiation failed" to get only lines with IP
# 1.4 made all inn to one loop, based on idea by rextended

:local logMessage ""
:local logIp ""
/log
:foreach i in=[find where message~"phase1 negotiation failed\\." or message~"SPI.*not regist" or message~"Invalid exchange"] do={
    :set logMessage [get $i message]

    :if ($logMessage~"phase1 negotiation failed\\.") do={
        :set logIp [:toip [:pick $logMessage -1 [:find $logMessage " "]]]
        :if ([:len [/ip fire addr find where list=IPSEC address=$logIp]] < 1) do={
            /ip fire addr add address=$logIp list=IPSEC timeout=7d
            :log info message="script=IPSEC_failed src_ip=$logIp why=negotiation_failed"
        }
    }

    :if ($logMessage~"SPI .* not registered for") do={
        :set logIp [:toip [:pick $logMessage ([:find $logMessage "for "]+4) [:find $logMessage "["]]]
        :if ([:len [/ip fire addr find where list=IPSEC address=$logIp]] < 1) do={
            /ip fire addr add address=$logIp list=IPSEC timeout=7d
			:log info message="script=IPSEC_failed src_ip=$logIp why=SPI_not_registered"
        }
    }

    :if ($logMessage~"Invalid exchange") do={
        :set logIp [:toip [:pick $logMessage ([:find $logMessage "for "]+4) [:find $logMessage "["]]]
        :if ([:len [/ip fire addr find where list=IPSEC address=$logIp]] < 1) do={
            /ip fire addr add address=$logIp list=IPSEC timeout=7d
			:log info message="script=IPSEC_failed src_ip=$logIp why=Invalid_exchange"
        }
    }
}