Dynamic-IP-Hacks Mini-HowTo Version 1.4.5 Moderated by: Michael Driscoll Featuring hacks from: Michael Driscoll Matthew Driver Matthew Nuckolls Justin Cragin Created Mon Apr 22 16:24:33 PST 1996 Last Updated Wed Sep 25 01:25:05 PDT 1996 If you just want to see the hacks skip down to section 2.0. 0.0 Table of Contents 1.0 Legalities, Definitions, &c. 1.1 Where to get the latest version of this HowTo 1.2 Feedback 1.3 Why this HowTo? 1.4 What is dynamic IP addressing and why do I have to put up with it? 1.5 Can you help me set up {pppd,telnetd,sendmail,&c.}? 1.6 Hey, what about SLIP? 1.7 Copyright 1.8 A Warning 2.0 Hack #1: Keeping your system up and accessible 24/7 2.1 What you'll need 2.2 Setting up keepalive.sh 2.2.1 Using diald instead of a crontab entry 2.3 ip-up and ip-down 2.3.1 Setting up your ip-up and ip-down 2.4 How to find out your new IP address from another computer 2.4.1 Telnetting to a POP3 server 3.0 Hack #2: Dynamically changing /etc/hosts 3.1 What you'll need 3.2 More fun with ip-up and ip-down! 4.0 Hacks #3-5: Various hacks from Ryan R. Klems 4.1 mail.c 4.2 pppdm.c 4.3 portmsg.c 5.0 Hack #6: Setting up a dynamic DNS entry by Matthew Driver 6.0 Hack #7: Updating your .plan by Matthew Nuckolls 7.0 Hack #8: A simple procmail recipe for finding your new IP by Justin Cragin 8.0 Hack #9: Dynamic Home Page via ftp by Michael Driscoll 8.1 Why would I want to do this? 8.2 Setting up your page 8.3 A simple search and replace Perl script 8.4 Editing .ncftprc 8.5 Calling it all via ip-up 9.0 Hack #10: Paging yourself with your new IP address by Michael Driscoll 9.1 The paging program 9.2 Calling it from ip-up Appendix A: We need more hacks! Appendix B: CREDITS ***1.0 Legalities, Definitions, &c. Section 1.2: Where to get the latest version of this HowTo The latest version of the Dynamic-IP-Hacks HowTo can be found at sunsite.unc.edu under the directory /pub/Linux/docs/howto/mini. Section 1.3: Feedback Feedback concerning this mini-HowTo should be addressed to Michael Driscoll . Section 1.3: Why this HowTo? The problem is, Linux just isn't as happy as it could be with dynamic IP addressing. It's the Unix equivalent of waking every morning to find that your postal address has changed and that all of your stationary is out of date. Unfortunately, ISP's are moving more and more towards this kind of addressing these days, and anyone who can't shell out more bucks for a stable IP address is just kinda stuck with this. The purpose of this HowTo, therefore, is to make your Linux box happier and more comfortable with dynamic IP addressing, which in turn should make you a bit happier and more comfortable. Section 1.4: What is dynamic IP addressing and why do I have to put up with it? First of all, I suppose I should explain just what an IP address is :) An IP address is a set of four numbers, each from 0 to 255, separated by a dot. An example would be 10.127.96.1. Each computer on the Internet has a unique IP address. The human-readable addresses that you probably use, like sunsite.unc.edu and bak2.lightspeed.net, are just semi-arbitrary names that are translated into their IP addresses by a DNS server whenever you try to access them. Dynamic IP addressing is something used by an ISP to cut down on the number of IP addresses that they need to "own". The way it works is, when you dial up your ISP, they simply give you the next IP number in their queue. This way, they don't need an IP address for every single customer they have, they just need one for every customer that might be online at any given time. Usually this is only about a tenth of their customer base at any given time. Why do they do this? Well, there just aren't enough IP addresses out there to give to every single customer of every single ISP on the planet, especially now that every computer user and his dog is getting connected. Besides, it doesn't affect Windows users at all, so why should they care? :) Therefore, it seems that unless you want to shell out some more money for a stable IP you should just try to make do with what you have. Section 1.5: Can you help me set up {pppd,telnetd,sendmail,&c.}? No. Figure it out yourself. Builds character :) Actually, it would be pretty pointless for me to reinvent the wheel by helping you set these up, as many documents already exist to help you out with these. Start by trying these: man pppd man chat man in.telnetd man inetd PPP-HowTo NET-2-HowTo NAG (Network Administrator's Guide by Olaf Kirch, available at fine LDP sites everywhere!) Section 1.6: Hey, what about SLIP? To tell you the truth, I didn't write about it here because I don't know it. Luckily, this shouldn't be too much of a problem as PPP seems to be the emerging standard, especially for dynamic IP addressing. Sorry if you're that 2% out there that has dynamic IP addressing with SLIP, but maybe you can glean a bit out of this HowTo and make your own setup (if you do then be sure to see Section 4 to get your hack included in this HowTo!) Section 1.7: Copyright Unless otherwise stated, Linux HowTo documents are copyrighted by their respective authors. Linux HowTo documents may be reproduced and distributed in whole or in part, in any medium physical or electronic, as long as this copyright notice is retained on all copies. Commercial redistribution is allowed and encouraged; however, the author would like to be notified of any such distributions. All translations, derivative works, or aggregate works incorporating any Linux HowTo documents must be covered under this copyright notice. That is, you may not produce a derivative work from a HowTo and impose additional restrictions on its distribution. Exceptions to these rules may be granted under certain conditions; please contact the Linux HowTo coordinator at the address given below. In short, we wish to promote dissemination of this information through as many channels as possible. However, we do wish to retain copyright on the HowTo documents, and would like to be notified of any plans to redistribute the HowTos. If you have questions, please contact Greg Hankins, the Linux HowTo coordinator, at gregh@sunsite.unc.edu via email. Section 1.8: A Warning This should go without saying, but I should say it anyways to cover things. I'm not sure all of this is the canonical way to do things, and if something I've done is just too weird let me know how to fix it. There might be security risks in all of this, but I don't know of them yet. This works on my machine, and I don't think it'll break yours too badly :) Either way, you should take care when doing this stuff and make sure you understand at least a bit about what this stuff is doing so you can fix it if it goes haywire. Also keep in mind this quote from the DNS-HOWTO by Nicolai Langfeldt : In this document I state flatly a couple of things that are not completely true (they are at least half truths though). All in the interest of simplification. Things will probably work if you believe what I say. ***************************** *The good stuff starts here!* ***************************** ***2.0 Hack #1: Keeping your system up and accessible 24/7 written by Michael Driscoll This hack involves a problem I have with my ISP. For reasons unknown to me (or apparently even to them) they like to hang up on me every five minutes to five hours. This was a problem for me because I wanted to be able to telnet to my Linux box from the computer in math class :) While it was easy enough to use a cron job to check the network status every few minutes and start pppd if it wasn't up, since my ISP uses dynamic addressing I couldn't find my address from the remote machine after this happened (except by luck). This hack keeps your PPP session up and puts your new IP number in an accessible place (your mail spool on your ISP's mail server). Section 2.1: What you'll need Not too much. A working PPP setup. A working sendmail (actually it just has to be working to the point where it can send a message to your ISP's mail server). A working telnetd/ftpd/inetd/whatever setup so you can actually get into your computer from somewhere else. A working cron. Section 2.2: Setting up keepalive.sh (note: This section can be probably be better with diald, see section 2.2.2) cron is a daemon that starts programs at specified times. You can look at your crontab by running "crontab -e". We'll be using crontab to run a shell program that will keep our PPP connection up. Run "crontab -e" as root and add this line: #*/2 * * * * /etc/ppp/keepalive.sh What this does is call the /etc/ppp/keepalive.sh script every two minutes. (The '#` comments out the entry so it won't start running the script until we are ready). Then put the following script named keepalive.sh in /etc/ppp: ------------------------------------------------------------------------- #!/bin/sh if [ -f /var/run/ppp0.pid ] ; then ping -c4 -l3 2>&1 | grep "0 packets" > /dev/null && \ { /usr/sbin/ppp-off > /dev/null 2>&1 ; sleep 2 ; /usr/sbin/pppd } else /usr/sbin/pppd fi ------------------------------------------------------------------------ (PS: This is an experimental script which replaces the old crontab entry I had here...if you see any problems with it, please let me know!) Now type "chmod 700 /etc/ppp/keepalive.sh" as root to make it an executable script. Hint1: Check those paths! For pppd and ppp-off insert whatever it is that you use to start/stop your connection. Hint2: I use your ISP's DNS server because I figure that if that's down, your net connection is screwed anyways :) Hint3: Be sure to use your DNS server's numeric IP address, otherwise ping returns a different message and the "grep" won't work. Now whenever you want your connection to stay up, you can just run crontab -e and take out that "#" to uncomment the entry...and when you want your connection to stay down, run crontab -e and put the "#" back in, then kill your connection with ppp-off or whatever it is that you use. I know there are other ways then this to keep a connection up (specifically I know that there is a script for pppd, and also a daemon called pppupd that will do it) but I have not tried them. If you use them and think them worthwhile, then drop me a note and I'll try them out. Section 2.2.1: Using diald instead of a crontab entry from Divya Mahajan Apparently, the crontab entry can also be done more elegantly (and more effeciently) using diald. I have left the crontab section in for reference and because it doesn't require an extra package, and when I try diald out I might switch the order of these two sections. What follows is an explanation of how to use diald to acheive a constant connection. (I don't actually use the program yet, the comments and script are courtesy of Divya). 1: Get the latest diald (diald-0.14.tar.gz probably in ftp://sunsite.unc.edu/pub/Linux/systems/Network/serial/diald-0.14.tar.gz) 2: Compile the stuff. (Actually you could probably get a precompiled diald package too from the Slackware sites) 3: After you have installed diald, you must modify /etc/diald.conf Add the following lines to the end: > restrict 06:00 19:00 * * * > up (This would force the link to be up between 6am to 7pm everyday. If you want 24hrs + 7days remove the restrict.) > device /dev/modem (Use the correct device;) > dynamic > reroute > connect-timeout 120 (Modify this if your dialer takes a longer or shorter time to connect to your ISP) > redial-timeout 10 (Interval between 2 redials) > defaultroute > accounting-log /var/adm/diald.log (Keep track of how much time you are using) >connect /path_to_myscript (You must use a dialing script so add the above line. When diald calls this script both standard input and standard output are redirected to /dev/modem (or whatever you chose above) so ensure that your dialer script doesn't print any garbage. I personally use "/usr/sbin/dip mydipfile.dip >> /var/adm/dip.log 2> /var/adm/dip.err" which logs the dial attempts. Initially you may want to run dip with the -v option to debug the dip file, i.e #dip -v mydipfile.dip. Remember to put the line "mode ppp" after you have logged in and started your PPP services at the ISP (Use dip-3.3.7n-uri). Also store all PPP settings in /etc/ppp/options rather than relying on the commandline. Once your DIP file is debugged and ready, its time to roll. Start up diald /usr/sbin/diald, if everything went fine it should start dialing and connect you to the ISP. When PPP shutsdown due to modem HUP, diald will automatically retry. Once you are confident, just put /usr/sbin/diald into your /etc/rc.d/rc.local) Now you have a 24hr PPP. diald is also good for a lot of other things, like demand dialing per port, etc. Look at the diald homepage (http://www.cs.toronto.edu/~schenk/diald.html) for some examples. Note: diald only takes the place of the crontab entry, the rest of this hack will still be needed if you want to make your ip address available for remote use. Section 2.3: ip-up and ip-down /etc/ppp/ip-up and /etc/ppp/ip-down are rather useful scripts for us. They are executed when pppd goes up/down and therefore are good for events that need to occur at this time. Mine look like this: ip-up: >#!/bin/sh >echo -n ^G > /dev/console >echo $4 > /var/run/add >mail -s "New IP address" fenris@lightspeed.net < /var/run/add >chmod 644 /var/run/add >echo "127.0.0.1 localhost" > /etc/hosts >echo $4 ulfheim.lightspeed.net ulfheim >> /etc/hosts ip-down: >#!/bin/sh >echo -n ^G > /dev/console >rm /var/run/add >echo 127.0.0.1 ulfheim.lightspeed.net localhost ulfheim > /etc/hosts Don't worry if you don't get all of this, we'll be explaining most of it right now. Section 2.3.1: Setting up your ip-up and ip-down The second part of this hack involves making your new IP address available to you from practically any machine on the net. To achieve this we do the following: Create a file as root called /etc/ppp/ip-up. Put this in it: #!/bin/sh # $4 is our new ip address passed by pppd # /var/run/add will hold our address echo $4 > /var/run/add # mail our address to our ISP's mail server, with the subject "new ip # address" mail -s "New IP address" yourname@your_internet_address < /var/run/add Then create /etc/ppp/ip-down and put this in it: #!/bin/sh rm /var/run/add Then run "chmod 700 /etc/ppp/ip-up /etc/ppp/ip-down" to make them executable. Now edit your rc startup scripts and put in a "rm /var/run/add" anywhere you feel is appropriate (just in case your power goes down while your connection is up). I put it in /etc/rc.d/rc.inet1, which should be okay. Voila! Now every time pppd is started your address will be mailed to your ISP's mail server. We retreive it in the next section. Section 2.4: How to find out your new IP address from another computer Well, this calls for some resourcefulness on your part. I really can't walk you through it, as it depends on which machine you're trying to get it from. In Windows, you can try to set up an email program to retrieve mail from your ISP's mail server, and if you're in Unix see if there's a program called "popclient" or somesuch. If all else fails, you can always use telnet (see the next section). If you have ftp access to your ISP's web server, then you can use Hack #9 to publish your new IP publically. Check Section 8 for details. Section 2.4.1: Telnetting to a POP3 server This is how I retrieve my IP address, as I am usually showing off when I do all of this anyways, and it really confuses all of the GUI users I show it to :) Hopefully your ISP uses a POP3 server for mail (most likely), otherwise you'll just have to figure this out yourself by looking up the RFC for the protocol you need and figure out how to do it by telnet. Anyways, first you want to telnet to port 110 of your ISP's mail server. In Unix you do this with "telnet your_isp's_mail_server 110", on a VAX you might need to do "telnet your_isp's_mail_server/port=110", and in a Web browser you should use "telnet://your_isp's_mail_server:110". Hopefully you can figure it out. Once you are connected, you should see something like this: >Connected to new-ls.lightspeed.net. >Escape character is '^]'. >+OK QUALCOMM Pop server derived from UCB (version 2.1.4-R3) at >new-ls.lightspeed.net starting. type "user your_username" to login. >+OK Password required for fenris. now type "pass your_password" >+OK fenris has 2 message(s) (3030 octets). type "list" to look at a list of your messages. >+OK 2 messages (3030 octets) >1 2400 >2 630 >. See that message with size 630? That's my IP address! How do I know? Because it's always that size :) Now type "retr message_number" to retrieve the message you want. >+OK 630 octets >Received: from ulfheim.lightspeed.net (avatar@bak2-pp-ls.lightspeed.net >[204.216.66.74]) by new-ls.lightspeed.net (8.6.12/8.6.12) with ESMTP id >TAA12048 for ; Mon, 22 Apr 1996 19:15:37 -0700 >Received: (from avatar@localhost) by ulfheim.lightspeed.net (8.7/8.6.9) >TAA00594 for fenris@lightspeed.net; Mon, 22 Apr 1996 19:15:29 >Date: Mon, 22 Apr 1996 19:15:29 -0700 >From: Deus In Machina >Message-Id: <199604230215.TAA00594@ulfheim.lightspeed.net> >To: fenris@lightspeed.net >Subject: New IP address > >204.216.66.74 > >. And there it is! Use "dele message_number" to get rid of it or just "quit" to leave it there and quit. Now just telnet to your machine at that address and have fun! If any of this is just not working, then check out RFC 1225, which describes the POP3 protocol in full detail. By the way, if this part of the hack doesn't work for you because you have cron automatically downloading your mail, then check out hack #8 which uses procmail to send your new IP address to any email address you want upon request. ***3.0 Hack #2: Dynamically changing /etc/hosts written by Michael Driscoll As I said before, Linux isn't completely happy with dynamic IP addressing. For example, sometimes talkd won't work with kludgy values in /etc/hosts. However, the following hack makes up for a lot of that by changing /etc/hosts according to the IP address we receive when we call up our ISP. For me this fixed problems with "hostname" and "talk". Section 3.1: What you'll need. Nothing but a working PPP setup, really. Section 3.2: More fun with ip-up and ip-down This hack is really very easy, and only involves three files, ip-up, ip-down, and an rc file (I put it in rc.S). ***NOTE***: First, make a backup of /etc/hosts, just in case this screws up anything. Also, if you have anything more complex than a two-line /etc/hosts or so, then this will need modifying. Add the following lines to /etc/ppp/ip-up: # echo 127.0.0.1 localhost > /etc/hosts echo $4 yourmachine.yourdomain yourmachine >> /etc/hosts # EXAMPLE: My machine is named "ulfheim" and its domain is "lightspeed.net". The lines in my ip-up say: # echo 127.0.0.1 localhost > /etc/hosts echo $4 ulfheim.lightspeed.net ulfheim >> /etc/hosts # Then add the following lines to /etc/ppp/ip-down: ------------------------------------------------------------------------ echo 127.0.0.1 yourmachine.yourdomain localhost yourmachine > /etc/hosts ------------------------------------------------------------------------ EXAMPLE: My machine is named "ulfheim" and its domain is "lightspeed.net". The lines in my ip-down say: ------------------------------------------------------------------------ echo 127.0.0.1 ulfheim.lightspeed.net localhost ulfheim > /etc/hosts ------------------------------------------------------------------------ Now put the following in an rc script (I chose rc.S, it seems ok there). ------------------------------------------------------------------------ echo 127.0.0.1 yourmachine.yourdomain localhost yourmachine > /etc/hosts ------------------------------------------------------------------------ EXAMPLE: My machine is named "ulfheim" and its domain is "lightspeed.net". The lines at the end of my /etc/rc.d/rc.S say: ------------------------------------------------------------------------ echo 127.0.0.1 ulfheim.lightspeed.net localhost ulfheim > /etc/hosts ------------------------------------------------------------------------ This last one is in case your power goes out or something while your connection was still up, it restores your offline /etc/hosts. ***4.0 Hacks #3-5: Various hacks from Ryan R. Klems intro written by Michael Driscoll code straight from Ryan R. Klems Here's some C code sent to me from Ryan R. Klems . There are three programs, the first, mail.c, is a CGI that scans through your mail spool for your new IP address as set up by Hack #1. It then uses the IP address to set up a page containing a link to this IP address. The second, pppdm.c, can probably take the place of hack #1, as it looks for a PPP connection, restarts pppd if it is down, and mails your new ip address to your ISP's mailserver. The third, portmsg.c, sits on a specified port and waits for a telnet connection. Upon connection, it will pull grep your mail spool for your newest IP and output a message containing that. These sources will need a bit of customization, so you probably shouldn't mess with them unless you know what they are doing. Oh yes, and Ryan has written to tell me that he wouldn't mind helping you set up the code to meet your needs, as long as you ask nicely :-) Section 4.1: mail.c /* * mail.c written by Ryan R. Klems (rklems@primenet.com) * Copyright 1996, Author releases this source freely, allowing * copying and modification, so long as the original copyright notice * is maintained. * * I request that if you use this file you mail me... Thats all I ask =) * * A CGI for reading through your mailfile and finding an IP * address that you had your computer mail to you. * * Compiling: * gcc mail.c -o mail.cgi * * Make sure to 'chmod +s mail.cgi' afterwards...must run with set uid * bit on to be able to open the mail file. */ #include #include #define MAILFILE "/var/mail/rklems" /* your mailfile */ main(void) { FILE *mail; /* file pointer for mail file */ char bob[80], location[80]; printf("Content-type: text/html\n\n"); printf("IP Address\n"); printf("\n"); printf("

IP Address

\n"); strcpy(bob, "42.**"); if((mail = fopen(MAILFILE, "r")) == NULL) printf("Mail file is empty or does not exist.\n"); else { /* * loop continues till end of file because you want most recent IP * 198.68. is the domain of my ISP, change to yours... */ while(!feof(mail)) /* until reaching EOF, do this */ { fgets(location, 80, mail); /* Grab a line, from mail */ sscanf(location, "198.68.%s", bob); /* look for domain */ } strcpy(location, "198.68."); strcat(location, bob); printf("The IP Address of your computer is: %s\n", location); } printf("\n"); } Section 4.2: pppdm.c /* * pppdm.c created by Ryan R. Klems (rklems@primenet.com) * Released freely by the author to use/modify/copy/reditribute * My only request is that if you use it...mail me and let me know =) * * This program keeps your link dialed up to an ISP and mails you * the newest IP address. Useful for people with Dynamically allocated * IP addresses * uses the following files... * /root/ip : Outputs the IP to this file * /root/log : If logging is defined * /root/pppchat : The chat file set up for chat. * My chat file looks like: * "" ATDT7917777 CONNECT "" "ogin:" "rklems" "assword:" "" * is YOUR password of course (like I'm gonna give you mine ;) * *NOTE* for silent dialing do ATMDT * * Compiling... * gcc pppdm.c -o pdm * *NOTE* Don't call it anything like pppdm b/c it looks for pppd * might accidentally kill itself off ;) */ #include #include #include #include #include #define DOLOG void main(void) { FILE *fin, /* multiuse file pointer */ *popen(); /* proto of popen() */ #ifdef DOLOG FILE *log; /* log file pointer */ #endif char line[80], /* a line of a file */ bah[80], /* just stuff, also used for holding IP addr */ crap[80], /* just stuff */ bob; /* single char placeholder, not used for anything */ int j, /* flag for if a link was found */ k, /* flag for if this is a new link */ pid; /* pid of pppd process to kill off */ #ifdef DOLOG time_t now; /* thing for time logging */ #endif for(;;) /* Loop forever... */ { /* look at ifconfig for IP addr */ if ((fin = popen("ifconfig", "r")) != NULL) while(fgets(line, 80, fin) != NULL) if(sscanf(line, "ppp0 %s", bah)) { fgets(line, 80, fin); sscanf(line, " inet addr:%15s", bah); j=1; } fclose(fin); if (!j) /* no link */ { if((fin = popen("ps -a -x", "r")) == NULL) { fprintf(stderr, "PPPdm error: cannot open file.\n"); exit(1); } /* scan through processes & kill off any zombie pppd processes */ while(fgets(line, 80, fin) != NULL) if (sscanf(line, "%d ? %c %4s pppd%s", &pid, &bob, crap, bah) == 4) kill(pid, SIGKILL); fclose(fin); k=0; /* new dial attempt */ system("pppd connect 'chat -v -f /root/pppchat'"); /* try again */ #ifdef DOLOG now = time(NULL); if ((log = fopen("/root/log", "a")) == NULL) { fprintf(stderr, "Error in opening log file.\n"); exit(1); } fprintf(log, "Initiating ppp-link. %s\n", ctime(&now)); fclose(log); #endif sleep(60); /* wait 1 min and check again */ } if(j && !k) /* first time with new address */ { if ((fin = fopen("/root/ip", "w")) == NULL) { fprintf(stderr, "Error in opening output file.\n"); exit(1); } fprintf(fin, "%s\n", bah); /* write out addr */ fclose(fin); /* mail it to yourself */ system("mail -s IP joker@your.moma.com < /root/ip"); k=1; } else /* take a nap and check again when we wake up */ { j = 0; sleep(300); /* wait 5 minutes to check again */ } } } Section 4.3: portmsg.c /* * Portmsg.c written by Ryan R. Klems (rklems@primenet.com) * Copyright 1996, Author releases this source freely, allowing * copying and modification, so long as the original copyright notice * is maintained. * * I request that if you use this program that you mail me. Thats * all I ask. * * This program sets up a port on a server to accept telnets. Upon * accepting a telnet, the program outputs a message, and then closes * the connection. * * address of message would be xxx.xxx.xxx.xxx yyyy where the x's * s the IP number or IP name, and yyyy is the port number set up * within this program. * * Compiling instructions: * Linux : gcc portmsg_gen.c -o * SunOS : gcc portmsg_gen.c -lsocket -lnsl -o * ***Note*** * I don't have access to any other operating systems, so if you * compile this program on an OS I don't have listed, and use * compiler options I didn't mention...please email me =) */ #include #include #include #include #include #include #include #include #include #include #define HOSTNAME "ares" /* hostname of computer */ #define PORT 3000 /* tcp port to bind to */ /* #define GETHOSTNAME */ /* uncomment if your server has gethostname() */ void get_location(void); /* proto of my mail reading function */ char location[80]; /* The IP address */ void main(void) { struct in_addr host_ip_number; struct sockaddr_in host_ip_addr; struct sockaddr_in addr; char host_name[100]; struct hostent *hp; int s, new_sock; int tmp, length; /* * The server I wrote this for doesn't have gethostname() * so, I put in a little fix... */ #ifdef GETHOSTNAME gethostname(host_name, sizeof(host_name)); #else strcpy(host_name, HOSTNAME); #endif hp = gethostbyname(host_name); bzero((char *)&host_ip_addr, sizeof(host_ip_addr)); memcpy((char *)&host_ip_addr.sin_addr, hp->h_addr, hp->h_length); host_ip_addr.sin_family = hp->h_addrtype; host_ip_number = host_ip_addr.sin_addr; host_ip_addr.sin_port = htons(PORT); host_ip_addr.sin_addr.s_addr = INADDR_ANY; /* open a socket s */ s = socket(host_ip_addr.sin_family, SOCK_STREAM, 0); if ((int)s==-1) { fprintf(stderr, "Error in opening socket.\n"); exit(1); } tmp = 1; if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&tmp, sizeof(tmp))<0) { fprintf(stderr, "Error in setsockopt.\n"); exit(1); } /* bind the socket to the server */ if (bind(s, (struct sockaddr *)&host_ip_addr, sizeof(host_ip_addr)) == -1) { if(errno == EADDRINUSE) { fprintf(stderr, "Socket already bound!\n"); exit(1); } else { fprintf(stderr, "Other error binding socket.\n"); exit(1); } } /* tell the server to listen to the port */ if (listen(s, 1) == -1) /* 1 is the maximum size of the connection queue */ { fprintf(stderr, "Error in listen.\n"); exit(1); } while(1) /* just keep looping */ { length = sizeof(addr); /* * port has been opened with socket(), bound with bind(), and set * active with listen(), now accept() watches the port for * connections, it will wait here until it has one... * new_sock is the file descriptor for the new socket */ new_sock = accept(s, (struct sockaddr *)&addr, &length); /* * The function get_location() and the send()'s are what I did to * suit my particular needs. Put your own messages in here... */ get_location(); /* send just sends a string foo of length strlen(foo) with flags */ send(new_sock, "Location:\n", 11, 0); send(new_sock, location, strlen(location), 0); close(new_sock); /* Close connection after message printed */ } } void get_location(void) { FILE *mail; /* file pointer for mail file */ char a[80]; /* char array for holding ip addr */ /* * FYI, this just opens my mail file, looks for a line with * 198.68.(the domain of my ISP), takes the last part, puts * the 198.68. in location, then cats the rest on the end */ strcpy(a, "42.**"); /* open /var/mail/ryan for read, and check to see there is a file */ if((mail = fopen("/var/mail/ryan", "r")) == NULL) { strcpy(location, "Error in obtaining information.\n"); return; } else { while(!feof(mail)) /* until reaching EOF, do this */ { fgets(location, 80, mail); /* Grab a line, from mail */ sscanf(location, "198.68.%s", a); /* look for domain */ } strcpy(location, "198.68."); strcat(location, a); /* loops continues till end of file because I want most recent IP */ } } ***5.0 Hack #6: Setting up a dynamic DNS entry by Matthew Driver There is already a page on this, at http://www.cfmeu.asn.au/matthew/virtualip.html. Check it out, Matthew just got it working and is asking for other people to try it to iron out the fine details. ***6.0 Hack #7: Updating your .plan by Matthew Nuckolls written by Matthew Nuckolls This pair of scripts allows one to put their current IP number in their .plan file on a remote server. I use it so friends and family can see if I'm dialed in, and where to send ytalk requests to. Put something like /etc/ppp/putip "None, the link is down" as the first line in your ppp-off script. You need a valid $HOME/.netrc file for putip to work. mine looks like: machine rocket login mnuck password and is chmod 600 -Matthew Nuckolls mnuck@umr.edu ----------ip-up: #!/bin/sh # # make sure this is chmod 711, since your password is stored in the clear PLANLOC = /home/mnuck/.plan REMOTE_USER_NAME = mnuck REMOTE_PASSWORD = REMOTE_PLANLOC = /afs/umr.edu/users/mnuck/pub/.plan REMOTE_SERVER = rocket echo "My dynamic IP is: " $4 > /tmp/ip.myip cat $PLANLOC /tmp/ip.myip > /tmp/plan echo $REMOTE_USER_NAME > /tmp/ip.script echo $REMOTE_PASSWORD >> /tmp/ip.script echo "put /tmp/plan" $REMOTE_PLANLOC >> /tmp/ip.script echo "quit" >> /tmp/ip.script ftp $REMOTE_SERVER < /tmp/ip.script &> /dev/null rm -f /tmp/ip.myip rm -f /tmp/ip.script rm -f /tmp/plan ----------end ip-up ----------putip: #!/bin/sh # # This script relies on a vaild .netrc file # -see ftp man page for details PLANLOC = /home/mnuck/.plan REMOTE_PLANLOC = /afs/umr.edu/users/mnuck/pub/.plan REMOTE_SERVER = rocket echo "My dynamic IP is: " $1 > /tmp/ip.myip cat $PLANLOC /tmp/ip.myip > /tmp/plan echo "put /tmp/plan" $REMOTE_PLANLOC > /tmp/ip.script echo "quit" >> /tmp/ip.script ftp $REMOTE_SERVER < /tmp/ip.script &> /dev/null rm -f /tmp/ip.myip rm -f /tmp/ip.script rm -f /tmp/plan ----------end putip ***7.0 Hack #8: A simple procmail recipe for finding your new IP by Justin Cragin written by Michael Driscoll Here's an easy one, assuming you already have procmail set up. This hack requires that you have a spare shell/email account where you can temporarily stick your mail and is useful when you can't use the mailing trick in hack one to get your new IP address because you have a cron job regularly downloading your mail. In your .procmailrc, simply add the following recipe: :0: * ^Subject: sendmeip |mail -s "Your new IP" other.mail.address@site.net < /var/run/add For the email address use a shell account whose mail is not being regularly downloaded by a cronjob to your local machine (what, doesn't anybody else collect shell accounts? I have four so far ;) /var/run/add is of course the file containing your IP address that we set up in hack one. Now to use this, just mail yourself from any place on the 'net with a subject of sendmeip, and then telnet to the shell account and wait for the cronjob on your machine at home to download that mail and automatically send you a reply with your new IP in it! ***8.0 Hack #9: Dynamic Home Page via ftp by Michael Driscoll This is a simple and easy hack for putting your new IP address on a home page on your ISP's http server. It requires: o ncftp (though you could kludge up a script that could use ftp) o perl (actually you could easily do the script in awk or C as well) o ftp access to your IPS's web server. You can see this hack in action at http://userzweb.lightspeed.net/~fenris (I also have scripts from for another hack that accomplishes the same thing that this does, though they use ftp and no perl. If you're interested in seeing them, send me mail asking for the "dynamic IP home page scripts"). Section 8.1: Why would I want to do this? 1: Because it's a neat hack. 2: So that all of your friends and family can easily find your new IP address (Ok, maybe that's not a good thing :) 3: So that you can run your own httpd, with your own CGI's (Merely put an http:// pointer to your new IP address which will access your own httpd) 4: When have we needed a reason to do something? Section 8.2: Setting up your page First, you're going to want to write up some HTML to go onto the pointer page. It is beyond the scope of this HowTo to teach you HTML, so you'll have to learn that bit yourself. However, when you write the page, in place of your machine address, instead put a recognizable anchor (I will use 'potrzebie' in this case) so that we can use a script to replace that anchor with our IP address. For example, a simple page would look like this: This is Mike Driscoll's Dynamic IP Dynamic Home Page

Last known IP of ulfheim.lightspeed.net:

potrzebie Notice that the href and the link name use 'potrzebie', which we will fill in with our IP address before we upload the page to the server. For this example, I'll assume that you've named this stock page /etc/ppp/index.html. Section 8.3: A simple search and replace Perl script Now we write the Perl script. Just put the following in /etc/ppp/makepage.pl: #!/usr/bin/perl $newip=$ARGV[0]; #We'll be calling the script with our new IP. while () { s/potrzebie/$newip/g; #Search and replace print; } Then "chmod 700" the script so that it can be executed. Section 8.4: Editing .ncftprc Now we'll add some stuff to root's .ncftprc. In mine, I have the following: ---------- #set auto-binary on #set recent-list off machine your.isps.web.server user yourusername password yourpassword macdef init cd /to/your/home/directory put /tmp/index.html index.html quit ---------- The auto-binary insures that we'll be in binary mode, the recent-list bit stops a .ncrecent file from being created (I just don't like them, you can have one if you want), and the machine entry defines my password, username, and default actions (you might not need the cd bit, at least I don't). Section 8.5: Calling it all via ip-up Now we'll be using that wonderful script, /etc/ppp/ip-up again. Merely add this to the end of ip-up: #Dynamic IP Home Page stuff follows if [! -x /tmp/index.html]; then /etc/ppp/makepage.pl $4 < /etc/ppp/index.html > /tmp/index.html ncftp your.isps.web.server 1>/dev/null 2>&1 rm /tmp/index.html else logger "Arrrgh, there's already a /tmp/index.html!" fi This bit of scripting first tests for the existance of /tmp/index.html (If we didn't, some hacker could make a link to any file on the file- system and it would be overwritten the next time this ran). If /tmp/index.html doesn't exist, then we run our stock page through makepage.pl, which we also call with $4 which holds our IP address (a feature of the ip-up script, remember?). It does the replacements, and out pops /tmp/index.html, which has our new IP address in it. We then run ncftp to connect to the web server, and it does its default action of uploading the contents of /tmp/index.html. We then remove /tmp/index.html. Congratulations, it's done, you now have a dynamic home page that you can use to point to your home machine! Have fun customizing it! ***9.0 Hack #10: Paging yourself with your new IP address by Michael Driscoll Here's a neat hack that I don't know anyone will use. It involves using a modem-dialing program to page oneself with one's new IP address. It requires: o modem (300 bps will do, we're just going to use it to dial) o extra phone line (doesn't need to be dedicated to this, as hopefully you have a modem that will gracefully give up if a voice call is already in progress (I guess that's something you should check, some modems are rather brutal about this)). o An ordinary numeric pager o A dialing program Section 9.1: The paging program For this hack you will need some program that has the ability to dial a modem from the command line. For the following examples, I will use the program 'modem-stats' by Kenneth J. Hendrickson , which can be found at ftp://sunsite.unc.edu/pub/Linux/apps/comm/modem-stats.tar.gz I imagine that other dialing programs can easily be adapted to these examples. Section 9.2: Calling it from ip-up We will put the following lines in /etc/ppp/ip-up: #Separate $4 (IP address) into four parts part1=`echo $4 | cut -f1 -d.` part2=`echo $4 | cut -f2 -d.` part3=`echo $4 | cut -f3 -d.` part4=`echo $4 | cut -f4 -d.` #Run dialing program /usr/sbin/modem-stats -c 'atz' /dev/cua2 /usr/sbin/modem-stats -c 'ats7=15' /dev/cua2 /usr/sbin/modem-stats -c 'atdt6384658,,,'${part1}'*'${part2}'*'${part3}'*' ${part4} /dev/cua2 Explanations: The atz resets the modem to sane defaults, the ats7=15 sets the time to wait for a carrier, the ','s are pauses, and the '*'s are used to signify dashes (at least they do on my pager, hopefully they will on yours). *Please note* that the last modem-stats line shouldn't really be broken in two, and there is no space between that '*' and ${part4} (I just put it that way because it's easier to see). Oh yes, and you might have better results if you put your own pager number in place of 638-4658, as that is my pager number :-) Also, replace /dev/cua2 with the device name for your modem. Congratulations. You're done! Appendix A: We need more hacks! If you have a Dynamic IP hack that you would like to contribute to this HowTo, then let me know. Be warned that if you do so that it will have to be covered under the copyright notice in Section 1.5, for reasons of CD publishing and whatnot. If you {liked this HowTo, didn't like this HowTo, thought this HowTo was too confusing, found something in this HowTo that doesn't work and/or is just plain wrong, want to send me email, don't want to send me email, found a security problem in this HowTo, etc} then please mail me at . I mean it, I really would appreciate *any* feedback on this HowTo, even if you just mailed me to say that you read it! Really! Just e-mail me on anything! I *really do* love getting e-mail! Flames will *not* go to /dev/null. I think if you feel that strongly about it, I should listen. If you write up a chapter for this HowTo not only will you be helping the Linux community, but you will receive full credit for your hack, your name will go into the credits at the end of this HowTo, you will be acknowledged as a contributor in the beginning of this HowTo, your idea will be archived with almost every CD Linux distribution in the world, you'll be the envy of all of your peers, etc. Appendix B: CREDITS Michael Driscoll is the HowTo moderator and the author of a few of the hacks. Christian G. Warden helped debug the keepalive.sh script used in hack one, now the thing will work in (hopefully) all cases, including those when the connection goes down but pppd does not. Justin Cragin gave me the idea for hack one, and then got mad when he found out I stole it. I also stole the message for my answering machine from him, so I guess I owe him this one. He also recently thought up a nice hack that has become hack eight, so he is now thrice credited. And he has now helped think of the idea of hack ten, so he is once again credited. Sheesh. gave me copies of some real neat scripts of his, see 8.0 for details. I seem to have lost his name, hopefully he'll send it to me again :) Ajit Deshpande wanted to be in the credits. Divya Mahajan sent me the info on diald, which could probably end up replacing the crontab entry in hack one. Ryan Klems sent me a bunch of his own hacks, see 4.x for details. Matthew Driver gave me a pointer to his page on dynamic DNS entries. Check out 5.0 for the URL. Matthew Nuckolls gave me hack seven, which updates a .plan containing your new IP address on a remote server via ftp. Scott Johnston showed me a better way to set up hack one (took out long and confusing crontab entry and put it in script called by cron instead), and gave me various other pointers.