Linode Forum
Linode Community Forums
 FAQFAQ    SearchSearch    MembersMembers      Register Register 
 LoginLogin [ Anonymous ] 
Post new topic  Reply to topic
Author Message
PostPosted: Fri Apr 28, 2017 4:35 pm 
Offline
Senior Newbie

Joined: Fri Mar 19, 2010 6:39 pm
Posts: 5
Hi,

I'm using pppoe connection with dynamic IP and most of the time I need to connect to my lab. (need to pay 3€/month extra for a static IP)
My pppoe connection is terminated on a mikrotik router box which is capable of running scripts on boot time.
So I've decided to find myself a solution. There was a solution in mikrotik wiki page but I didn't want to use dyndns.

Step 1:

For the domain choose minimal TTL values.

Step 2:

Create an A record on the domain with some dummy IP.

Step 3:

Here is the mikrotik code, put that into system\scripts:
Code:
:local username "admin" #change this
:local password "your-password-goes-here" #change this
:local hostname "kozy-sol" #change this

:global previousIP
:global enforceUpdate

#to debug:
:set enforceUpdate true

# print some debug info
:log info ("UpdateDNS: username = $username")
:log info ("UpdateDNS: hostname = $hostname")
:log info ("UpdateDNS: previousIP = $previousIP")

# get the current IP address from the internet
/tool fetch mode=http address="yourdomain.com" src-path="/currentip.php" dst-path="/ip.html" #change this
:local result [/file get ip.html contents]

# parse the current IP result
:local resultLen [:len $result]
:local startLoc [:find $result ": " -1]
:set startLoc ($startLoc + 2)
:local endLoc [:find $result "</body>" -1]
:local currentIP [:pick $result $startLoc $endLoc]
:log info "UpdateDNS: currentIP = $currentIP"

# Determine if dns update is needed
:if (($currentIP != $previousIP) || ($enforceUpdate = true)) do={
    :set enforceUpdate false
    :set previousIP $currentIP
    /tool fetch user=$username password=$password mode=http address="yourdomain.com" src-path="/updateip.php?hostname=$hostname&ip=$currentIP" dst-path="/dnsupdate.txt" #change this
    :local result [/file get dnsupdate.txt contents]
    :log info ("UpdateDNS: Update needed")
    :log info ("UpdateDNS: Update Result: ".$result)
    :put ("Dns Update Result: ".$result)
} else={
    :log info ("UpdateDNS: No update needed")
}


Step 4:

Put this file as updateip.php

Code:
<?php
        $username = "admin"; #change this
        $password = "your-password-goes-here"; #change this
        $key = "your_linode_api_key"; #change this
        $ip = $_GET['ip'];

        if(!isset($_SERVER['PHP_AUTH_USER']))
        {
                header("WWW-Authenticate: Basic realm=\"Mikrotik update\"");
                header("HTTP/1.0 401 Unauthorized");
                echo "HTTP 401 Unauthorized";
                exit;
        }
        else if($_SERVER['PHP_AUTH_USER'] == $username && $_SERVER['PHP_AUTH_PW'] == $password)
        {
                $time = time();
                echo "IP updated: " . $ip . " @ " . $time;
                $url = "https://api.linode.com/?api_key=".$key;
                $url .= "&api_action=domain.resource.update&DomainID=your-domain-id-goes-here&ResourceID=your-a-record-id-goes-here&Target=".$ip; #change this
                $response = file_get_contents($url);
                # echo $response;
        }
        else
        {
                echo "Authentication failed";
        }
?>



Step 5:

Put this file as currentip.php

Code:
<html><head><title>Current IP</title></head><body>Current IP Address: <?=$_SERVER['REMOTE_ADDR'];?></body></html>


Please note that I'll update this tutorial with more description, better code and screenshot if anyone is interested.
For the first day, I wanna see if this is something that needed by anyone else.

Thanks for reading!

Resource:
https://wiki.mikrotik.com/wiki/Dynamic_ ... behind_NAT


Top
   
PostPosted: Fri Apr 28, 2017 4:53 pm 
Offline
Linode Staff
User avatar

Joined: Tue Apr 15, 2003 6:24 pm
Posts: 3123
Website: http://www.linode.com/
Location: Galloway, NJ
Cool. Suggestion:

Code:
# get the current IP address from the internet

There is no need for this, or the external script to get your IP address.

https://www.linode.com/api/dns/domain.resource.update for Target: "When Type=A or AAAA the token of '[remote_addr]' will be substituted with the IP address of the request.".

So, simply set target="[remote_addr]" (literally - no substitution) and the API will assign the target to the remote_addr of the request.

-Chris


Top
   
PostPosted: Fri Apr 28, 2017 5:36 pm 
Offline
Senior Newbie

Joined: Fri Mar 19, 2010 6:39 pm
Posts: 5
caker wrote:
Cool. Suggestion:

Code:
# get the current IP address from the internet

There is no need for this, or the external script to get your IP address.

https://www.linode.com/api/dns/domain.resource.update for Target: "When Type=A or AAAA the token of '[remote_addr]' will be substituted with the IP address of the request.".

So, simply set target="[remote_addr]" (literally - no substitution) and the API will assign the target to the remote_addr of the request.

-Chris


I was also thinking of this revision :)


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
RSS

Powered by phpBB® Forum Software © phpBB Group