DroidScript updating Dynamic DNS through No-IP service

83 views
Skip to first unread message

Eric Geer

unread,
Dec 21, 2016, 7:19:57 PM12/21/16
to DroidScript
Hi Folks,

First off, DISCLAIMER: Method is NOT secure, so please use at your own risk.  Your password will post over the Internet in clear text.  This is for demonstration purposes only.

Some of you may be dealing with scripts on your phone for which you want to remotely connect back to.  Easiest way to do this would be to setup a dynamic DNS service such as No-IP and have it updated whenever it's changed.  There are apps that do this, but I've seen some inconsistencies with them, and decided to do a quick example to post how to do this from within DroidScript so you can make sure your domain is updated as your scripts require.

// Update No-IP service with your current public IP
// Written by Eric Geer Dec-21-2016 courtesy of HTTP Get Sample

var username = "My_Username_Here";            //Replace My_Username_Here with your login for No-IP service
var password = "My_Password_Here";                  //Replace My_Password_Here with your password for No-IP service
var domain = "My_Hostname_Here.ddns.net";     //Replace My_Hostname_Here with host name including the ddns.net. if your username is the same as the host, you can use it to append ddns.net by appending variables
var publicip;                               //you can use a service like icanhazip.com to give you your public IP address
var noIP = "http://" + username + ":" + password + "@dynupdate.no-ip.com/nic/update?hostname=" + domain + "&myip=" + publicip;
var lastip;                                 //this will match the last public IP so that the script can run periodically to know to update
var site;                                   //Will be used to distinguish the connection
//Send request to remote server.
SendRequest( "http://icanhazip.com" );             //Get current public IP address

//Send an http get request.
function SendRequest( url )
{
    if ( url == "http://icanhazip.com" ) site = url;
    if ( url == noIP ) site = noIP;
    var httpRequest = new XMLHttpRequest();
    httpRequest.onreadystatechange = function() { HandleReply(httpRequest); };
    httpRequest.open("GET", url, true);
    httpRequest.send(null);
}

//Handle the servers reply (a json object).
function HandleReply( httpRequest )
{
    if( httpRequest.readyState==4 )
    {
        //If we got a valid response.
        if( httpRequest.status==200 )
        {
            console.log( "Connecting: " + site );
            reply = httpRequest.responseText;
            console.log( reply );
            if ( site == "http://icanhazip.com" ) {
                publicip = reply;
                console.log( "Current public address is: " + publicip );

//Update No-IP service if IP changed
                if ( lastip != publicip ) {
                    console.log( "Last public IP was: " + lastip );
                    console.log( "Current public IP is: " + publicip );
                    SendRequest( noIP );
                }
//reset IP so as not to update unless necesary
                lastip = publicip;
            }
            else console.log( "Updating public IP information: " + reply );
        }
        //An error occurred
        else
            console.log( "Error: " + httpRequest.status + httpRequest.responseText);
    }
}

Munawar Hussain

unread,
Aug 4, 2020, 11:30:21 AM8/4/20
to DroidScript
Tried this code with my details, but screen showed nothing, blank screen appear, web browser debug provided current public ip.
Reply all
Reply to author
Forward
0 new messages