Printable Version of Topic

Click here to view this topic in its original format

Linuxhelp _ Tips and Tricks _ Server On A Non-static Ip

Posted by: Jim May 6 2004, 11:59 AM

So I don't have a tip or trick on how to do this, but I want someone to help me make one.

So I want to be able to host a ssh/apache server from my home, which has a semi-static IP address. It doesn't really change all that often, but they reserve the right to so it does every now and than. So this won't be such a big deal this summer as I will be able to have the computer infront of me if the IP changes. Whats going to screw me is when I am away from school.

So the question is any ideas on how to deal with a semi-static IP? My thought was having some sort of auto-scheduled task to e-mail me the IP when it reboots. Because it wont get new IP unless I do a network restart or a reboot on it. Than I could always have the IP. I don't even know if thats possible and or how I would do that.

Getting a staic IP isn't a financial option or even possible since they don't offer them where I am at and on home services. So how do you host a server on a semi-static IP address?

Posted by: Termina May 6 2004, 01:14 PM

First, you'll probally want a free domain name. biggrin.gif

www.DynDNS.com

Add a Dynamic DNS domain.

Get something like "lest.ath.cx"

You can get a program that gets your IP and updates your domain name here: http://s90389134.onlinehome.us/ddclient/ddclient.tar.gz

If that doesn't work, there are more found here: http://www.dyndns.org/services/dyndns/clients.html


Also, I'm pretty sure that when our ISP changes your IP on you, your linux machine doesn't need to restart. Atleast in my experiance it updates itself on it's own.

Posted by: chrisw May 6 2004, 03:57 PM

what is a semi static ip address? biggrin.gif ...that is new to me...something i forgot to read about...or would that the same as dhcp with a specific
length lease on the ip.....ya i know you dont have to go explaining it...just picking....


just kidding not meaning to cause any trouble

Posted by: hughesjr May 6 2004, 06:30 PM

I use the free http://www.zoneedit.com/doc/dynamic.html on zoneedit.com ... but you have to provide a real domain name (which you can buy for less than $10.00/year).

Posted by: Jim May 6 2004, 09:33 PM

Thats exactly what I need, thanks.

And chris, ok so "semi-static" might not be a techincal term, but you know what I ment. My IP address might not change for a month, but than just one day it will randomly up and change, thats the issue.

Posted by: Corey May 23 2004, 01:23 PM

If you have a domain name, does Zoneedit cost anything to use? This may be perfect for my domain with an IP that changes every couple of days.

Posted by: hughesjr May 23 2004, 03:17 PM

zoneedit is free for up to 5 domains ... as long as you have the domain name.

I've been using it for 4 years with no problems.

Posted by: Corey May 24 2004, 12:22 PM

Hughsjr: Okay, so I own my own domain, and I have a dynamic ip that changes every couple of days. So, with zoneedit, you can just download software t hat will update your ip with their system?

My DSL hooks into my D-Link router which initiates the PPPoE connection, will the software work in that setup do you know?

I've read the site, but i don't fully understand it.

Posted by: Termina May 24 2004, 12:43 PM

That is correct. smile.gif

I'm going to assume you use linux for your webserver. wink.gif

You could put one of these in cron (or in a bash script that cron runs):
lynx -source -auth=username:password 'http://dynamic.zoneedit.com/auth/dynamic.html?host=www.mydomain.com'

wget -O - --http-user=username --http-passwd=password 'http://dynamic.zoneedit.com/auth/dynamic.html?host=www.mydomain.com'

If you want to have minimal down time (if your IP changes), you should probally have it set to run that command every 30 minutes to an hour.

Or you could use this program (I use the above lynx option, myself... easier smile.gif)

http://members.rogers.com/ddclient/pub/ddclient.tar.gz

Posted by: hughesjr May 24 2004, 01:56 PM

Yep ... I wrote a script that checks my IP address and looks up what the name server at zone edit says it is supposed to be ... and if they don't match, it updates the IP on their DNS server.

I don't have it avialable from here, but I'll post it when I get home....

Posted by: Termina May 26 2004, 01:05 AM

Does anyone ever notice that when someone (myself included) says they're going to post something when they get home, they never do? wink.gif

*laughs*


Then again, I'm a terribly forgetful person. =D

Posted by: hughesjr May 26 2004, 06:05 AM

Well .... with some prodding from termina smile.gif .... here is my update script (actually, two scripts):

The first script is called check_ip and I call it every 15 minutes ... it checks my IP address on eth0 (my external NIC on my Linux firewall) and then does a name lookup at the zoneedit site. If the IPs match, it does nothing ... if they don't, it updates the website with the text browser lynx.

If I can't get a name lookup at all ... (that is, $DOMAIN_IP is blank) then I restart my network and restart my firewall ... my ISP requires my cable modem to download it's configuration file at a certian interval ... and after it downloads, I can't talk to the internet until I do another DHCP request ... so this part of the script does that for me).

check_ip

CODE
#!/bin/bash

###############################
#Full Path to executables
#use the command "which ifconfig", etc. to find the info on your system ...
#this is correct for most RedHat distros

IFCONFIG=/sbin/ifconfig
AWK=/bin/awk
GREP=/bin/grep
DIG=/usr/bin/dig
LYNX=/usr/bin/lynx
ECHO=/bin/echo
###############################

###############################
#External Interface (if on this machine)

EXTIF="eth0"
###############################

###############################
#Domain name to Monitor (you only need 1 for all domains in this IP)

DOMAIN_NAME="www.hughesjr.com"
###############################

###############################
#Primary DNS listed

PRIMARY_DNS="ns5.zoneedit.com"
###############################

###############################
#Domains to update (enter all domains with a space between them)
#Example DOMAINS_TO_UPDATE="domain1.com www.domain1.com ftp.domain1.com domain2.org"

DOMAINS_TO_UPDATE="hughesjr.com www.hughesjr.com mail.hughesjr.com"
###############################

###############################
#ZoneEdit username and password
USERNAME="your_username_here"
PASSWORD="your_zoneedit_password_here"
###############################

###############################
#Get the External IP address
#
# If the machine you are running this script on is your firewall,
# use the IFCONFIG line to find your external IP address.  If you have
# an external firewall, comment out the IFCONFIG EXTIP line (with a #)
# and use the LYNX (remove the #) instead.
#
EXTIP="`$IFCONFIG $EXTIF | $AWK /$EXTIF/'{next}//{split($0,a,":");split(a[2],a," ");print a[1];exit}'`"
#EXTIP="`$LYNX -source http://www.whatismyip.com/ | $GREP 'TITLE' | $AWK '{split($0,a,"is");split(a[2],a," ");print a[1];exit}'`"
###############################

#####---------------------------------------------######
#####----------------SCRIPT BEGINS----------------######
#####-No changes normally needed below this point-######
#####---------------------------------------------######

#Date for Logs
DATE="`date +'%x %X %Z - '`"


#Get the Domain Name's IP Address
DOMAIN_IP="`$DIG @$PRIMARY_DNS $DOMAIN_NAME | grep $DOMAIN_NAME | grep -v DiG | grep -v ";"$DOMAIN_NAME | awk '{print $5}'`"

###############################
#If there is an error, log it and exit the script
#otherwise, continue with the updates

if [ "$DOMAIN_IP"x == x ]; then
 $ECHO $DATE"Error doing lookup!" >> /var/log/check_ip.log
 /etc/init.d/network restart
 /etc/rc.d/rc.firewall
 exit 1
else
 ###################
 #This is for testing, or a more verbose output to the screen
 #$ECHO "External IP: "$EXTIP
 #$ECHO "Monitored IP: "$DOMAIN_IP
 #$ECHO "Domains to update: "$DOMAINS_TO_UPDATE
 ###################

 #Actual update is required ... update domains and log it
 if [ "$EXTIP" == "$DOMAIN_IP" ]; then
   #$ECHO $DATE"External IP("$EXTIP") matches Domain IP("$DOMAIN_IP"). No Action required."
   exit 0
 else

   for DOMAIN in $DOMAINS_TO_UPDATE

   do
     $ECHO $DATE"Updating "$DOMAIN" from "$DOMAIN_IP" to "$EXTIP >> /var/log/check_ip.log
     COMMAND="$LYNX -source -auth=$USERNAME:$PASSWORD http://dynamic.zoneedit.com/auth/dynamic.html?host=$DOMAIN"
     $COMMAND
   done
 fi
fi


The other script is one that I run once a month (it is called monthly_update_ip. Even if your IP hasn't changed, if it isn't updated at least once a month then zoneedit will close your account (because they figure you have lost interest in maintaining your site). SO this script is basically the same as the other script ... but it does the update even if the looked up IP is the same as the external IP.

monthly_update_ip
CODE
#!/bin/bash

###############################
#Full Path to executables
#use the command "which ifconfig", etc. to find the info on your system ...
#this is correct for most RedHat distros

IFCONFIG=/sbin/ifconfig
AWK=/bin/awk
GREP=/bin/grep
DIG=/usr/bin/dig
LYNX=/usr/bin/lynx
ECHO=/bin/echo
###############################

###############################
#External Interface (if on this machine)

EXTIF="eth0"
###############################

###############################
#Domain name to Monitor (you only need 1 for all domains in this IP)

DOMAIN_NAME="www.hughesjr.com"
###############################

###############################
#Primary DNS listed

PRIMARY_DNS="ns5.zoneedit.com"
###############################

###############################
#Domains to update (enter all domains with a space between them)
#Example DOMAINS_TO_UPDATE="domain1.com www.domain1.com ftp.domain1.com domain2.org"

DOMAINS_TO_UPDATE="hughesjr.com www.hughesjr.com mail.hughesjr.com"

###############################

###############################
#ZoneEdit username and password
USERNAME="your_username_here"
PASSWORD="your_zoneedit_password_here"
###############################

###############################
#Get the External IP address
#
# If the machine you are running this script on is your firewall,
# use the IFCONFIG line to find your external IP address.  If you have
# an external firewall, comment out the IFCONFIG EXTIP line (with a #)
# and use the LYNX (remove the #) instead.
#
EXTIP="`$IFCONFIG $EXTIF | $AWK /$EXTIF/'{next}//{split($0,a,":");split(a[2],a," ");print a[1];exit}'`"
#EXTIP="`$LYNX -source http://www.whatismyip.com/ | $GREP 'TITLE' | $AWK '{split($0,a,"is");split(a[2],a," ");print a[1];exit}'`"
###############################
#####---------------------------------------------######
#####----------------SCRIPT BEGINS----------------######
#####-No changes normally needed below this point-######
#####---------------------------------------------######

#Date for Logs
DATE="`date +'%x %X %Z - '`"

#Get the Domain Name's IP Address
DOMAIN_IP="`$DIG @$PRIMARY_DNS $DOMAIN_NAME | grep $DOMAIN_NAME | grep -v DiG | grep -v ";"$DOMAIN_NAME | awk '{print $5}'`"

###############################
#If there is an error, log it and exit the script
#otherwise, continue with the updates

if [ "$DOMAIN_IP"x == x ]; then
 $ECHO $DATE"Error doing lookup!" >> /var/log/check_ip.log
 exit 1
else
 ###################
 #This is for testing, or a more verbose output to the screen
 #$ECHO "External IP: "$EXTIP
 #$ECHO "Monitored IP: "$DOMAIN_IP
 #$ECHO "Domains to update: "$DOMAINS_TO_UPDATE
 ###################

 #Actual update is required ... update domains and log it
 for DOMAIN in $DOMAINS_TO_UPDATE
   do
     $ECHO $DATE"Updating "$DOMAIN" from "$DOMAIN_IP" to "$EXTIP >> /var/log/check_ip.log
     COMMAND="$LYNX -source -auth=$USERNAME:$PASSWORD http://dynamic.zoneedit.com/auth/dynamic.html?host=$DOMAIN"
     $COMMAND
   done
fi


If you have an external router ... you need to figure out how to get the external IP address from it. Most have a URL that you can use to do that.

There are 3 long lines in the code that might wrap ... they are both a single line ... here they are:

DOMAIN_IP="`$DIG @$PRIMARY_DNS $DOMAIN_NAME | grep $DOMAIN_NAME | grep -v DiG | grep -v ";"$DOMAIN_NAME | awk '{print $5}'`"

the EXTIP lines are also one line....

and
CODE
COMMAND="$LYNX -source -auth=$USERNAME:$PASSWORD http://dynamic.zoneedit.com/auth/dynamic.html?host=$DOMAIN"


########################EDITED##########################
OK ---- I edited the file and added a second option for EXTIP if you have an external router.....

Posted by: hughesjr May 26 2004, 06:17 AM

Here is what the cron looks like that runs these:

05 03 3 * * /usr/local/bin/monthly_update_ip >> /dev/null 2>&1
02,17,32,47 * * * * /usr/local/bin/check_ip >> /dev/null 2>&1


The first one is 3rd day of the month at 03:05 ... the other runs every hour at 02, 17, 32, and 47 after the hour.

In case you are wondering why my times are not 00,15,30,45 ... it is because the system already does things at those times (schedulaed by the system ... or on a multiple user machine, the other users) so when I schedule things, I try to offest them slightly, to minimize stuff happening all at the same time. It's just a habit I have developed as a sysadmin ... so I do it even at home on a machine that only I have access to!

Posted by: Corey May 26 2004, 08:05 AM

Hugh,
I don't know bash scripting at all, so, can you do me a favour and tell me how i can grep out my externel ip using the website "http://www.myip.com/" ?

Posted by: Corey May 26 2004, 09:05 AM

Okay, i don't know awk or anything, but I did try fooling around with it, and came up with the following. This returns your externel IP (useful if you are behind a router because your eth0 interface will only give you your local IP address.

I know this can be made smarter and shorter, so please let me know.

[quote]
lynx -source http://www.myip.com | awk 'BEGIN {FS=" "};{print $4}' | awk 'BEGIN {FS="<"};{print $1}
[/quote]




edit:
Okay, when i got home i set this up, I get the following output:

[quote]
eulogy cron.d # sh check_ip.sh
check_ip.sh: command substitution: line 1: unexpected EOF while looking for matching `''
check_ip.sh: command substitution: line 2: syntax error: unexpected end of file
<SUCCESS CODE="200" TEXT="Update succeeded." ZONE="thegoodtimes.ca" HOST="thegoodtimes.ca" IP="142.163.151.255"><SUCCESS CODE="201" TEXT="No records need updating." ZONE="thegoodtimes.ca" HOST="www.thegoodtimes.ca">
eulogy cron.d #
[/quote]

Lines 1 and 2 are:

[quote]
#!/bin/bash

[/quote]

So, I should probably just ignore those errors right? it looks like it still worked.

Posted by: hughesjr May 26 2004, 07:37 PM

Corey,
MyIP.com is always overloaded ... at least for me ... but whatismyip.com is usually faster.

This works:

CODE
lynx -source http://www.whatismyip.com/ | grep 'TITLE' | awk '{split($0,a,"is");split(a[2],a," ");print a[1];exit}'


so in the context of my script, you can remove the line:

CODE
###############################
#External Interface (if on this machine)

EXTIF="eth0"


and replace:

CODE
EXTIP="`$IFCONFIG $EXTIF | $AWK /$EXTIF/'{next}//{split($0,a,":");split(a[2],a," ");print a[1];exit}'`"


with

CODE
EXTIP="`$LYNX -source http://www.whatismyip.com/ | $GREP 'TITLE' | $AWK '{split($0,a,"is");split(a[2],a," ");print a[1];exit}'`"


and it should work


#################EDITED#############################

I edited the original scripts (earlier in this post) to include a second option to do a Domain lookup if you have an external router ... or if you are running it on the router.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)