Community discussions

MikroTik App
 
User avatar
own3r1138
Long time Member
Long time Member
Topic Author
Posts: 680
Joined: Sun Feb 14, 2021 12:33 am
Location: Pleiades
Contact:

Tunnel broker API script

Wed May 31, 2023 5:59 pm

I welcome any suggestions to improve the script.
2023-05-31_18-09-18.jpg
.
:local  IPv6 "true"
:local APIurl "https://USER:TOKEN@ipv4.tunnelbroker.net/nic/update?hostname=TUNNEL-ID&myip="
:global WANInterface "PPPOE"
:local previousIP ""
:local WANip ""
:local ipN ""
:local APIn
:local APIu
:set $APIn "true"
:set $APIu "true"


:if ($IPv6 = "true") do={
    :local currentIP ([/interface pppoe-client monitor [find name=$WANInterface] once as-value]->"local-address")
    :set WANip $currentIP
    :set  ipN $currentIP
}

:if ([/file find name=disk/db/tb.tmp.txt] = "") do={
    :log error "No previous ip address file found, createing..."
    :set previousIP $WANip
    :execute script=":put $WANip" file="/disk/db/tb.tmp"
    #:error message="No previous ip address file found."
    :log warning ("TB API: Updating, please wait! Setting = $WANip")
    :do {/tool fetch output=none url="$APIurl$WANip"} on-error={
     :log error "IPv6: API is down!"
     :set $APIn "false"
    }
:if ($APIn=true) do={
:log warning ("TB API: $WANip IP address seted.")
} 
:set $APIn true
} else={
    :if ( [/file get [/file find name=disk/db/tb.tmp.txt] size] > 0 ) do={ 
    :global content [/file get [/file find name="disk/db/tb.tmp.txt"] contents] 
    :global contentLen [ :len $content ] ; 
    :global lineEnd 0
    :global line "";
    :global lastEnd 0 
            :set lineEnd [:find $content "\n" $lastEnd ] 
            :set line [:pick $content $lastEnd $lineEnd] 
            :set lastEnd ( $lineEnd + 1 ) 
            :if ( [:pick $line 0 1] != "#" ) do={   
                #:local previousIP [:pick $line 0 $lineEnd ]
                :set previousIP [:pick $line 0 $lineEnd ];
                :set previousIP [:pick $previousIP 0 [:find $previousIP "\r"]]
            }
    }
}
:if ($previousIP != $WANip) do={
    :do {/tool fetch output=none url="$APIurl$WANip"} on-error={
    :log error "IPv6: API is down!"
    :set $APIu false
    }
:if ($APIu=true) do={
    :log warning ("TB API: $WANip IP address updated.")
    /ip dns cache flush
    :if ( [/file get [/file find name=disk/db/tb.tmp.txt] size] > 0 ) do={
        /file remove disk/db/tb.tmp.txt
        :execute script=":put $WANip" file="/disk/db/tb.tmp"        
    }
  :set $APIu true
  }
}
You do not have the required permissions to view the files attached to this post.
Last edited by own3r1138 on Fri Jun 16, 2023 8:54 pm, edited 2 times in total.
 
User avatar
own3r1138
Long time Member
Long time Member
Topic Author
Posts: 680
Joined: Sun Feb 14, 2021 12:33 am
Location: Pleiades
Contact:

Re: Tunnel broker API script

Wed May 31, 2023 7:09 pm

I can NOT delete this.

Who is online

Users browsing this forum: Kanzler, nichky and 22 guests