Community discussions

 
User avatar
soonwai
Member Candidate
Member Candidate
Topic Author
Posts: 162
Joined: Mon Feb 06, 2012 10:50 pm
Location: Kuala Lumpur

[Script] Namecheap Dynamic DNS Update Script

Thu Apr 14, 2016 8:44 pm

Namecheap Dynamic DNS Update Script
http://wiki.mikrotik.com/wiki/Dynamic_D ... _Namecheap

This is a script which checks if the IP for an interface has changed, updates namecheap.com's dynamic DNS and also sends you an email about it. :o

As it turns out, it's also good for letting me know when electricity has been restored after a blackout or when the ISP comes back from an outage. :)

ROS now has /ip cloud with Mikrotik's own ddns. With this script and Namecheap, you can have a much cooler and shorter ddns domain. And also a backup in case /ip cloud is down.

My first script here so please let me know if things can be improved. I'm quite the newbie to Mikrotik. The script was originally taken from the Mikrotik wiki for another ddns provider and then modified for Namecheap.

Email tool needs to be setup beforehand. Just change the globals to yours. A few things are hardcoded: interface name to check, email address & smtp server. There's no error checking if the ddns update fails or sending email encounters an error. Schedule this script to run every few minutes as you please.
:global ddnsserv "dynamicdns.park-your-domain.com"
:global ddnshostname "cctv1"
:global ddnsdomain "soonwai.com"
:global ddnspass "44274h3e71dbe7cd18e1d8ab5877267d5ac3"

:global ddnsip
:global ddnslastip
:global strdate [/system clock get date]
:global strtime [/system clock get time]
:global strsystemname [/system identity get name]
:global strsystemuptime [/system resource get uptime]
:global strsystemfreemem [/system resource get free-memory]
:global strsystemcpuload [/system resource get cpu-load]
:global ddnssystem ("Version-" . [/system package get system version] )

:global ddnsip [ /ip address get [/ip address find interface=pppoe-out1 ] address ]

# Strip the net mask off the IP address
:for i from=( [:len $ddnsip] - 1) to=0 do={
    :if ( [:pick $ddnsip $i] = "/") do={ 
        :set ddnsip [:pick $ddnsip 0 $i]
       } 
   }

:if ([ :typeof $ddnslastip ] = nil ) do={ :global ddnslastip "0" }
:if ([ :typeof $ddnsip ] = nil ) do={
   :log info ("DDNS: No ip address present on pppoe interface, dammit TM.")
} else={
  :if ($ddnsip != $ddnslastip) do={
    :log info ("DDNS: Updating $ddnsip $ddnslastip")
    :local str "/update?host=$ddnshostname&domain=$ddnsdomain&password=$ddnspass&ip=$ddnsip"
    /tool fetch address=$ddnsserv src-path=$str mode=http dst-path=("/disk1/DynDNS.".$ddnshostname)
    :log info "DDNS: Sending Email"
    /tool e-mail send to=soonwai@example.com subject="IP Address $strdate $strtime $strsystemname" body="$strsystemname $strdate $strtime \r$ddnshostname.$ddnsdomain \r$ddnssystem\rNew IP: $ddnsip \rPrevious IP: $ddnslastip \rUptime: $strsystemuptime \rFree memory: $strsystemfreemem kb \rCPU Load: $strsystemcpuload % " start-tls=yes server=[:resolve smtp.gmail.com]
    :global ddnslastip "$ddnsip"
  } else={ 
#    :log info "DDNS: No update required."
    }
}
Last edited by soonwai on Sat Mar 04, 2017 9:07 pm, edited 3 times in total.
 
User avatar
soonwai
Member Candidate
Member Candidate
Topic Author
Posts: 162
Joined: Mon Feb 06, 2012 10:50 pm
Location: Kuala Lumpur

Re: Namecheap Dynamic DNS Update Script

Mon Feb 20, 2017 9:25 pm

 
IMLizKing
just joined
Posts: 1
Joined: Mon Oct 23, 2017 8:03 pm

Re: [Script] Namecheap Dynamic DNS Update Script

Tue Oct 24, 2017 1:38 am

I used the above as a starting point for my own namecheap dynamic DNS update script. I tied it into my DHCP client script for my WAN interface instead, so there is no need to run it every 5 minutes or anything like that. I also didn't bother with the e-mail or keeping a pile of variables globally. Anyways, I thought I'd share it here since this is where I ended up when I wanted dynamic DNS with namecheap on my MikroTik router...

Code: Select all

:if ($bound=1) do {

# the name (within the domain) to update -- must already exist!
:local host <dns hostname>

# the domain name
:local domain <dns domainname>

# key for namecheap updates
:local password <namecheap key>

# get wan IP (easy in the DHCP script)
:local wanip $"lease-address"

:log info "DHCP got WAN IP of $wanip"

:local url "https://dynamicdns.park-your-domain.com/update?host=$host&domain=$domain&password=$password&ip=$wanip"

#:log info "URL args: $url"

# this should to the deed! (note: no verification is done here, check namecheap yourself at first!)
/tool fetch url=$url mode=https keep-result=no

} else {
#:log info "DHCP was unbound"
}
You'll want to login to namecheap first, lookup your DNS key for updates, and make an initial entry in your DNS table (of type "A + Dynamic DNS Record".)

The script doesn't do much (any) error checking, but I figure if it works once, it'll probably keep working. And I don't do anything when the DHCP is unbound.. I don't think that hurts much of anything, plus I don't know that we can remove a DNS entry remotely on namecheap.

Hope this helps someone! Cheers!
 
User avatar
CyberCam
just joined
Posts: 1
Joined: Fri Jul 20, 2018 1:04 pm

Re: [Script] Namecheap Dynamic DNS Update Script

Fri Aug 03, 2018 1:42 pm

So if you have more than one host name do you need to recreate this script for each sub domain host name? Or is there a way to enter all your sub domain host name in one script!
 
BloodLizard
just joined
Posts: 1
Joined: Mon Jan 14, 2019 3:30 pm

Re: [Script] Namecheap Dynamic DNS Update Script

Mon Jan 14, 2019 3:38 pm

So if you have more than one host name do you need to recreate this script for each sub domain host name? Or is there a way to enter all your sub domain host name in one script!
Did some simple modifications for myself.

# the name (within the domain) to update -- must already exist!
:local hosts [:toarray value="record1,record2,record3"];
# the domain name
:local domain <domain>
# key for namecheap updates
:local password <pass key>
# get wan IP
:local ddnsip [ /ip address get [/ip address find interface=<wan interface> ] address ]
:log info "WAN IP is $ddnsip"
# Strip the net mask off the IP address
:for i from=( [:len $ddnsip] - 1) to=0 do={
    :if ( [:pick $ddnsip $i] = "/") do={ 
        :set ddnsip [:pick $ddnsip 0 $i]
       } 
   }
:foreach host in $hosts do={
:local url "https://dynamicdns.park-your-domain.com/update?host=$host&domain=$domain&password=$password&ip=$ddnsip"
:log info "URL args: $url"
/tool fetch url=$url mode=https keep-result=no
}

Who is online

Users browsing this forum: No registered users and 2 guests