"Linux Gazette...making Linux just a little more fun! "


Setting up dynamic IP web server via PPP connection

By Henry H. Lu, honglu@rt66.com, https://www.tc.umn.edu/nlhome/m508/luxxx012/

Copyright (c) 1996
Published in Issue 10 of the Linux Gazette

Have you been thinking of seting up a reachable web server at your home PC in addition to your permenent page at ISP? There are obvious reasons to do this: You can show off your home linux box to the world; you do not need to use other messy method (email) to know your current IP in order to login remotely; finally, it is fun!

First, You need to have ppp connection and httpd working and a PERMANENT web page before trying the following dynamic IP solution.

Description of files:

web_up: shell script I run to update webpage at permenet site with new IP whenever connection is up.

web_down: shell script I run before shutting down the link, to inform others of the shutdown

update_uppage: perl scripts to creat up.html page with updated IP address on the fly, called by web_up.

up.html_source: fixed part of up.html

down.html: web page used when link is down.

/etc/add, /etc/last_add: files where I put IP address.

ip-down, ip-up: files executed when ppp link is disconnected or connected. they are used to update the /etc/add files here.

Now lets look at scripts web_up:

------------------------------------------------------------------

    #!/bin/sh
    #check new IP
    new_ip()
    {
    if [ -f /etc/add ]; then
       if [ -f /etc/last_add ]; then
          if /usr/bin/diff /etc/add /etc/last_add >/dev/null ; then
              exit 1 
          else
              return 0 
          fi
       else
          return 0
       fi
    else
       exit 1
    fi
    }

    #check whether maroon is connected
    try_connect()
    {
    if ping -c4 -l3 128.101.118.21  2>&1 | grep "0 packets" > /dev/null
    then 
       return 1
    else
       return 0
    fi
    }
                    
    if try_connect
    then
         touch  /var/run/maroon_connected  
    else
         rm -f /var/run/maroon_connected
    fi

    # ftp to update page
    if [ -f /var/run/maroon_connected ] && new_ip
    then
       # update_uppage is perl scripts, exit status is opposite of shell
       if ( ! /home/honglu/public_html/update_uppage )
       then 
          cd /home/honglu/public_html
          if echo "put up.html /nlhome/m508/luxxx012/dynamic.html" \
              | /usr/bin/ftp maroon  
          then 
             rm -f /etc/last_add
             cp /etc/add  /etc/last_add
             exit 0
          else
             exit 1
          fi
       fi
    else
        exit 1
    fi

-----------------------------------------------------------------

Now let's look at web_up in detail.

Function new_ip() is used to check whether we have new IP and whether the new IP is different from the last one. /etc/ppp/ip-up and /etc/ppp/ip-down update IP adress in files /etc/add and /etc/last_add so that we can compare files "add" with "last_add" to tell whether we need to update page.

Function try_connect() is used to test whether the perment web site is reachable.

Next is fun part, I used automatic feature of ftp to update webpage. In order to make it work, you have to set file ~/.netrc correctly, type "man ftp" for more information.

update_uppage is straitforward perl scripts to parse and creat up.html by using new IP from /etc/add file.

Final part is to update /etc/add /etc/last_add to reflect correct status IP address.

You can put "web_up" in your crontab entry ( or ip-up, or keapalive.sh) to let it execute automatically whenever your PC is connected.

Web_down is a similar page, main difference is in ftp part:

-----------------------------------------------------
......
......
# ftp to send down.html page 
if [ -f /var/run/maroon_connected ] 
then
      cd /home/honglu/public_html
      if  echo "put down.html /nlhome/m508/luxxx012/dynamic.html" \
        | /usr/bin/ftp maroon  
      then
          rm -f /etc/last_add
      else
          exit 1
      fi
else
      exit 1
fi

----------------------------------------------------

Instead of ftp up.html as web_up did, web_down put down.html to permenent web site to inform the delink of page.

web_down should be run before you shut down the machine. I created a scripts called "shut" to shutdown machine:

-----------------------------------------
#!/bin/sh
if web_down
then
   shutdown -h now
else
   echo "can not web_down"
   exit 1
fi
-----------------------------------------

For more detail check out my home page for source code:

https://www.tc.umn.edu/nlhome/g625/luxxx024/

Henry H Lu


[ TABLE OF CONTENTS ] [ FRONT PAGE ]  Back  Next