Daily Ubuntu / Linux news and application reviews.

ads

A few days ago, I wrote an article on how to use your home computer to get past internet restrictions with SSH but I didn't cover the aspect of the computer running the SSH sever not having a static IP. So here is how to use DynDNS hostname for your dynamic IP.

The first thing you need to do is create an https://dyndns.com/ account and then go to My account > My hosts > add a dynamic dns and pick some hostname such as myhomepc.homelinux.com or whatever you want. This will point to your current IP but if your IP is dynamic, it will change so we need to update this info in DynDNS automatically.

For this, we'll create a script in your /etc/cron.hourly/ folder:

sudo gedit /etc/cron.hourly/dyndns

There are 2 different versions for this script: bash and python, use whichever you want from below, but make sure the file name is dyndns without extension (no .py) because the scripts in cron.hourly won't run if they have an extension:

1. BASH:
#!/bin/bash
# Set the username and password for the dyndns account
USERNAME=
PASSWORD=
# Set the system being used to either static or dynamic DNS
SYSTEM=dyndns
# Set the hostname for the record to change
DYNHOST=
# Set whether to wildcard the DNS entry, i.e. *.$DYNHOST
WILDCARD=OFF
############################################
## DO NOT EDIT ANYTHING BEYOND THIS POINT ##
############################################
if [ -z "$DYNDNS" ]; then
DYNDNS="$DYNHOST"
fi
if [ -z "$DNSWILD"]; then
DNSWILD="$WILDCARD"
fi
LOOKUP=`host $DYNHOST | awk '{print $4}'`
MYIP=`curl -s http://www.ipchicken.com | awk '/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/ {print $1}'`
# Do the work
if [ "$LOOKUP" = "$MYIP" ]; then
echo "No change in DNS entry."
else
echo `lynx -auth=${USERNAME}:${PASSWORD} -source "http://members.dyndns.org:8245/nic/update?system=${SYSTEM}&hostname=${DYNDNS}&myip=${MYIP}&wildcard=${DNSWILD}"`
fi

Next to "USERNAME=" and "PASSWORD=", fill in your DynDNS username and password and save the file.

2. Python:

#!/usr/bin/env python

import os
import socket
import urllib
import urllib2
import re

#Put your own username, password and dynhost name here.
username='your_user_name'
password='your_password'
dynhost='yourdomain.whatever.com'
system='dyndns'
wildcard='OFF'

############################################
## DO NOT EDIT ANYTHING BEYOND THIS POINT ##
############################################

print '\nDyndns.org updater is running!!!\n'

#Find out what dynamic IP is currently set to.
lookup=socket.gethostbyname(dynhost)
print 'DynDNS is set to' , lookup

#Find out what external IP really is.
ipchicken = urllib2.urlopen('http://www.ipchicken.com')
chicken = ipchicken.read(10000000)
ipchicken.close()
rawstr = r"""[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*""" #regex for an IP address
myip_obj = re.search(rawstr, chicken)
myip = myip_obj.group()
print 'Your actual IP is' , myip

#Compare those IPs
if lookup == myip :
print 'No change in DNS entry.'
else:
#change the DNS entry

data = {}
data['system'] = system
data['hostname'] = dynhost
data['wildcard'] = wildcard
data['myip'] = myip
url_values = urllib.urlencode(data)

url = 'http://members.dyndns.org:8245/nic/update'
full_url = url + '?' + url_values

# create a password manager for loging into dyndns.org
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()

# Add the username and password.
password_mgr.add_password(None, url, username, password)

handler = urllib2.HTTPBasicAuthHandler(password_mgr)

# create "opener" (OpenerDirector instance)
opener = urllib2.build_opener(handler)

# use the opener to fetch a URL
opener.open(full_url)

print "DNS updated."


Don't forget to 'your_user_name', 'your_password' and 'yourdomain.whatever.com' with your info.

Doesn't matter which script you choose from above, now you must make the script executable by running the following command:
chmod a+x /etc/cron.hourly/dyndns


From now on you can login to your ssh server with ssh user@myhomepc.homelinux.net (or whatever hostname you choose when you set this up - in the beginning of the post). Even if your ISP changes your IP address while you are away, your IP will be updated to DyDNS hourly.

Credits: celticmonkey user @ubuntuforums.org.
«
Next
Newer Post
»
Previous
Older Post