After deciding to host my website using Namecheap’s hosting service, I found that their hosting DNS servers don’t support Dynamic DNS (unlike their default nameservers).
The point of this is to (at the very least) be able to update and remotely retrieve the IP address of a server that does not have a static IP (and without using a dynamic DNS service, because that would be too easy).
What you need
- A website that is hosted remotely
- PHP
Setup
Generate auth key
To prevent anyone and everyone from being able to update your “dynamic” IP address on your website, an authorization key is required for the IP address to be updated. If no authorization key is used or if the wrong one is used, the last updated IP is echoed.
Instead of coming up with your own password, you can get a 20 character alphanumeric string at RANDOM.ORG; or you can use any other random string generator of your choice (the longer the better as long as it’s random).
SHA-512 is used to hash the password that’s used to update the server’s IP address. In this example, the password is “password” and the SHA-512 hash used in the PHP code can be obtained using echo -n "password" | shasum -a 512
.
From a security standpoint, this whole setup isn’t great; but at the very least the password you use won’t be available to everyone as plain text if your server’s PHP installation stops working for some reason.
PHP code
Here’s the PHP code to put in the index.php file on the server. Just replace the hash of the password with the actual hash (the current hash below is the SHA-512 hash of “password’, so that should be updated).
<?
#CHANGE THIS TO THE HASH OF YOUR PASSWORD
$authKey = 'b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cacbc86';
$queryString = $_SERVER['QUERY_STRING'];
parse_str($queryString, $output);
$parsedKey = hash("sha512", $output['key']);
$updateIP = $_SERVER["REMOTE_ADDR"];
if ($parsedKey==$authKey)
{
#update ip.txt if authKey is valid
$File = 'ip.txt';
$Handle = fopen($File, 'w');
fwrite($Handle, $updateIP);
fclose($Handle);
}
#always output the current IP
echo file_get_contents('ip.txt');
?>
updating the IP
You can update the URL using either a web browser, or more likely, using the command line. To update the IP address using a browser, simply enter the URL followed by ?key=password
(replacing “password” with your actual password, of course).
For example, if I created a subdomain called “server”, I would update the IP by going to server.example.com/?key=password
in the browser.
To update the IP from the terminal:
curl server.example.com/?key=password
This can be automated by updating /etc/crontab to update the IP address every two hours (or however often you want to update the server’s IP address):
0 */2 * * * root /usr/bin/curl server.example.com/?key=password > /dev/null 2>&1
Note: It might be worthwhile to add some logic to prevent the IP from being updated when it doesn’t need to be. This could be done by checking to see what the client’s external IP address is, comparing that against what’s returned by server.example.com, and updating as necessary.
Using the dynamic IP
Connecting with SSH
IP="`curl -s server.example.com`" ; ssh user@$IP -p 22
Using the same example subdomain as earlier, this command would use the IP address specified at server.example.com to connect as the “user” user, using port 22 (which is the default, but could be something else depending on how the SSH server is set up).