Community discussions

MikroTik App
 
hobbes1069
newbie
Topic Author
Posts: 29
Joined: Sun Aug 16, 2015 3:43 pm

Can't find the error

Fri Feb 16, 2018 3:32 pm

I have an almost working script that updates the IP for Dyndns using the https method instead of the legacy http method. It also uses an API token instead of your password.

The problem is there's some sort of error I can't figure out. /system script print highlights the brace in front of the last else statement but I can't figure out for the life of me what the problem is.
### Settings ###
[redacted]

### Script Begins ###
:global ipfresh [ /ip address get [/ip address find interface=$theinterface ] address ]
:global ipddns [:resolve $ddnshost]

# All return codes from members.dyndns.org
:global resultcodes {good="Update successful";
    nochg="WARNING: Update not needed, repeated attempts may cause you to be blocked.";
    badauth="The username and password pair do not match a real user.";
    notfqdn="The hostname specified is not a fully-qualified domain name.";
    nohost="The hostname specified does not exist in this user account.";
    numhost="Too many hosts (more than 20) specified in an update.";
    abuse="The hostname specified is blocked for update abuse.";
    badagent="The user agent was not sent or HTTP method is not permitted.";
    dnserr="DNS error encountered.";
    911="There is a problem or scheduled maintenance on our side."
}

# Check ip address and remove subnet.
:if ([ :typeof $ipfresh ] = nil ) do={
    :log info ("DynDNS: No ip address on $theinterface .")
} else={
    :for i from=( [:len $ipfresh] - 1) to=0 do={ 
        :if ( [:pick $ipfresh $i] = "/") do={ 
            :set ipfresh [:pick $ipfresh 0 $i];
        }
    }
}

# Determine if an update is required.
:if ($ipddns != $ipfresh) do={
    :local logname "dyndns.$ddnshost"
    :log info ("DynDNS: IP-DynDNS = $ipddns")
    :log info ("DynDNS: IP-Fresh = $ipfresh")
    :log info "DynDNS: Update IP needed, saving UPDATE...!"
    /tool fetch url="https://$ddnsuser:$ddnstoken@members.dyndns.org/v3/update?hostname=$ddnshost&myip=$ipfresh" keep-result=yes dst-path=$logname;
    :local result [ /file get [ /file find name="$logname" ] contents ]
    /file remove [ /file get [ /file find name="$logname" ]
    :put "Server returned: $result";
    # Separate return code from IP address
    :for i from=0 to=( [:len $result] - 1) do={ 
        :if ( [:pick $result $i] = " ") do={
        :set result [:pick $result 0 $i]
        }
    }
    :put "Return code is: $result";
    :foreach resultcode,desc in=$resultcodes do={
        :if ( $result = $resultcode ) do={
        :log info ("DynDNS: $desc")
        }
    }
    :log info ("DynDNS: IP updated to $ipfresh!")
} else={
    :log info ("DynDNS: IP-DynDNS = $ipddns")
    :log info ("DynDNS: IP-Fresh = $ipfresh")
    :log info "DynDNS: no update required"
}
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7196
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Can't find the error

Fri Feb 16, 2018 3:50 pm

There is clearly missing something in this line:

/file remove [ /file get [ /file find name="$logname" ]

It is not complete


And by the way you can write shorter:
:local result [/file get [find name="$logname" ] contents ]
/file remove [ find name="$logname" ]
 
hobbes1069
newbie
Topic Author
Posts: 29
Joined: Sun Aug 16, 2015 3:43 pm

Re: Can't find the error

Fri Feb 16, 2018 5:33 pm

THANK YOU!

That got it.