Community discussions

MikroTik App
 
markrudling
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 51
Joined: Tue Aug 15, 2006 6:28 pm

DynDns Script help - removing subnet from result

Fri Jan 04, 2013 10:31 pm

Hi guys

I have an issue with my one Internet Provider. Internet traffic is passed through web proxy so the web proxy IP is passed to dyndns when trying get ip address.

For this reason, i need to get the ip address of the active interface and send that to dyndns. I have re-worked the script below and it works, with only one issue.
The IP Address is set to xxx.xxx.xxx.xxx/16. When this address is sent to dyndns, it fails as the formatting is incorrect, so dyndns then reads the ip address of in the header of the html packet, again, this is of the web proxy.

my solution is to strip the subnet and send the ipaddress only to dyndns.

Full Script
# Define User Variables
:global ddnsuser "USERNAME"
:global ddnspass "PASSWORD"
:global ddnshost "DNS.HOST"

# Define Global Variables
:global ddnsip
:global ddnslastip
:if ([ :typeof $ddnslastip ] = "nothing" ) do={ :global ddnslastip "0" }

:global ddnsinterface
:global ddnssystem ("mt-" . [/system package get system version] )

# Define Local Variables
:local int

# Loop thru interfaces and look for ones containing
# default gateways without routing-marks
:foreach int in=[/ip route find dst-address=0.0.0.0/0 active=yes ] do={
  :if ([:typeof [/ip route get $int routing-mark ]] != str ) do={
     :global ddnsinterface [/ip route get $int gateway]
  }
}

# Grab the current IP address on that interface.
:global ddnsip [ /ip address get [/ip address find interface=$ddnsinterface ] address ]

# Did we get an IP address to compare?
:if ([ :typeof $ddnsip ] = "nothing" ) do={
   :log info ("DynDNS: No ip address present on " . $ddnsinterface . ", please check.")
} else={
  :if ($ddnsip != $ddnslastip) do={
    :log info "DynDNS: Sending UPDATE!"
    :local str "/nic/update?hostname=$ddnshost&myip=$ddnsip&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"
    /tool fetch address=members.dyndns.org src-path=$str mode=http user=$ddnsuser \
        password=$ddnspass dst-path=("/DynDNS.".$ddnshost)
    :delay 1
    :local str [/file find name="DynDNS.$ddnshost"];
    /file remove $str
    :global ddnslastip $ddnsip
  }
}
This works find, but sends the subnet with the address here:
    :local str "/nic/update?hostname=$ddnshost&myip=$[b]ddnsip[/b]&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"
My solution was to add
:global ddnssendip
and strip subnet with
for i from=( [:len $ddnsip] - 1) to=0 do={ :if ( [:pick $ddnsip $i] = "/") do={ :put [:pick $ddnsip 0 $i]}}
the stripping works if i run it in terminal, but i cant get the resulted striped ip to be set as a global variable

please help with this last part.
 
User avatar
skot
Long time Member
Long time Member
Posts: 584
Joined: Wed Nov 30, 2011 3:05 am

Re: DynDns Script help - removing subnet from result

Sat Jan 05, 2013 2:07 am

The :put command only displays output in the terminal. Use the :set command to set the ddnssendip global variable.
for i from=( [:len $ddnsip] - 1) to=0 do={ :if ( [:pick $ddnsip $i] = "/") do={ :set ddnssendip [:pick $ddnsip 0 $i]}}