Community discussions

MikroTik App
 
andrielau
just joined
Topic Author
Posts: 2
Joined: Tue Jul 07, 2020 12:33 pm

Script does not run with a gateway rto condition and restarts wireguard

Thu May 18, 2023 2:12 pm

I have several scripts with the same point, restarting the wireguard interface
# Local variable deviceName to get Mikrotik Identity
:local deviceName [/system identity get name]
# Local variable deviceDate to get Mikrotik Date
:local deviceDate [/system clock get date]
# Local variable deviceDate to get Mikrotik Time
:local deviceTime [/system clock get time]

# BOT ID
:local bot "1234567890:abcdefghijklmnopqrstuvwxyz123456789"

# Group ID
:local group "-0123456789012"

# Local variable textSent to save what message you want to send to Telegram Bot
:local textSent ""

# Local variable Ketoprak to Zeus
:local WG_1 "%0ARestart Wireguard Peer VPN_1"
# Local variable Ketoprak to Lake
:local WG_2 "%0ARestart Wireguard Peer VPN_2"

# VPN_1 #
:local a 0;
:local pingSuccess false;
:while (($a < 5) && (!$pingSuccess)) do={
    :set pingSuccess ([/ping 192.168.1.1 interface=vpn01 interval=2 count=1]=0);
    :if (!$pingSuccess) do={
        :log warning "Unable to ping VPN_1";
    }
    :set a ($a + 1);
};
# Found 5 consecutive errors during previous PING command
:if ($pingSuccess) do={
    # Combine textSent and WG_1
    :set textSent ($textSent . $WG_1)
    # Restart WG
    /interface/wireguard/peers/disable numbers=0
    :delay 2s
    /interface/wireguard/peers/enable numbers=0
    :set textSent ("Router ID : $deviceName%0A$deviceDate $deviceTime%0A" . $textSent)
    /tool fetch url="https://api.telegram.org/bot$bot/sendmessage?chat_id=$group&text=$textSent" keep-result=no;
} else {
    :log error "Unable to ping VPN_1 after 5 attempts, skipping command...";
}

# VPN_2 #
:local b 0;
:local pingSuccess false;
:while (($b < 5) && (!$pingSuccess)) do={
    :set pingSuccess ([/ping 192.168.10.1 interface=vpn02 interval=2 count=1]=0);
    :if (!$pingSuccess) do={
        :log warning "Unable to ping VPN_2";
    }
    :set b ($b + 1);
};
# Found 5 consecutive errors during previous PING command
:if ($pingSuccess) do={
    # Combine textSent and WG_2
    :set textSent ($textSent . $WG_2)
    # Restart WG
    /interface/wireguard/peers/disable numbers=1
    :delay 2s
    /interface/wireguard/peers/enable numbers=1
    :set textSent ("Router ID : $deviceName%0A$deviceDate $deviceTime%0A" . $textSent)
    /tool fetch url="https://api.telegram.org/bot$bot/sendmessage?chat_id=$group&text=$textSent" keep-result=no;
} else {
    :log error "Unable to ping VPN_2 after 5 attempts, skipping command...";
}
that script cannot run properly. Then I made a simple script but with the same result, still cannot run properly
:local destination1 "192.168.1.1"   # Destination 1 IP to ping
:local destination2 "192.168.10.1"   # Destination 2 IP to ping
:local interface1 "vpn01"      # Interface 1 to restart if timeout occurs
:local interface2 "vpn02"      # Interface 2 to restart if timeout occurs

:log info "Ping monitor script started"

:delay 5s  # Initial delay before starting the loop

:while (true) do={
  :local pingStatus1 [/ping $destination1 interface=$interface1 count=1 interval=1s]
  :local pingStatus2 [/ping $destination2 interface=$interface2 count=1 interval=1s]

  :if ($pingStatus1~"timeout") do={
    :log warning "Ping timeout detected for $destination1. Restarting interface $interface1."
    /interface/wireguard/peers/disable numbers=0
    :delay 2s  # Delay before enabling the interface again
    /interface/wireguard/peers/enable numbers=0
    :log info "Interface $interface1 restarted."
  }

  :if ($pingStatus2~"timeout") do={
    :log warning "Ping timeout detected for $destination2. Restarting interface $interface2."
    /interface/wireguard/peers/disable numbers=1
    :delay 2s  # Delay before enabling the interface again
    /interface/wireguard/peers/enable numbers=1
    :log info "Interface $interface2 restarted."
  }
}
Can someone here help? I really appreciate and thank you very much

Who is online

Users browsing this forum: No registered users and 20 guests