home
Doug Klimesh's DUNconf: Batch file and Perl script to change IP address in httpd.conf for Win9x

With Windows 95 & 98 one now has the power to run for free both Perl (ActivePerl) and the Apache server. This combination allows you to run CGI scripts on your computer. Both programs are relatively easy to install and set up for running Perl/CGI scripts on your computer. There is even a short tutorial to help you with this process. However if your Perl script accesses a computer on the Internet, then you must set the ServerName directive in Apache's httpd.conf file to your IP address. With dial up networking this adds a slight complication, because most likely your Internet Service Provider issues you a different IP address each time you connect.

So the steps are: Make your Dial Up Networking (DUN) connection. Run the winipcfg program (it should have been installed with your Windows 9x installation in C:\Windows) to tell you your current IP address. Edit the httpd.conf file and edit the ServerName line to include the IP address. (Delete the leading "#" if it is there.) Then start up Apache. Your Perl scripts should now be able to access the Internet. (It seems like Apache should be able to figure out the IP address on its own, but I haven't been able get Apache to do this.)

This situation is screaming out for a programming solution:

First save the following as a DOS batch file called DUNconf.bat:

@echo off
winipcfg /Batch C:\WINIPCFG.OUT
perl c:\perl\bin\dunconf.pl C:\WINIPCFG.OUT
if errorlevel 1 goto error
C:\apache\Apache.exe -d c:\APACHE -s
cls
:error

Make sure the line 5 Apache startup command is correct. (You can obtain it from the Apache startup shortcut's properties in Windows\StartMenu\Programs\...) You can call and place the winipcfg output file, C:\WINIPCFG.OUT, anything you like as long as it is the same in both lines 2 and 3. Also make sure the path to dunconf.pl in line 3 points to where you save the following program.


Save the following Perl program as dunconf.pl:

#!C:\Perl\bin\perl.exe
#
# dunconf.pl by Doug Klimesh 8-99
#
# This program changes your server httpd.conf file ServerName directive to the IP
# address given by winipcfg. Connect with your Windows 9x dial-up-networking to
# obtain your IP address then run the DOS batch file.
#
$conf = 'C:\apache\conf\httpd.conf';
$srvname = 'ServerName';
#
open (INFOFILE, $ARGV[0]) || die ("Can't open file $ARGV[0]\n");
@IPinfofile = <INFOFILE>;
$IP = substr($IPinfofile[5],index($IPinfofile[5],':')+2);
chop ($IP);
if ($IP eq "0.0.0.0"){
    die ("You are not connected. Connect first and then run this program again.\n");}
open (CONF, $conf) || die ("Can't open file $conf\n");
@confcont = <CONF>;
close CONF;
$i = 0;
while ($i <= @confcont){
    if ($confcont[$i] =~ /$srvname/ && $confcont[$i] !~ /^ *\#/){
        last; }
    else {
        $i++;
}    }
if ($i > @confcont){
    print "$srvname not defined in file $conf. Will append.\n";
    open (CONF, ">>$conf");
    print CONF ("$srvname $IP\n");
}else {
    open (CONF, ">$conf");
    $confcont[$i] = $srvname . " " . $IP . "\n";
    print CONF (@confcont);
    print "Changed line $i in file $conf \n";
}
exit(0);

Change line 1 to the path to your perl.exe, and change line 9 to the path to your httpd.conf file. (I assume the output of winipcfg is exactly the same for all versions of winipcfg.)

To run it just connect to your ISP, run the batch file, and you're ready to start running your Perl CGI scripts! To shutdown Apache run the shutdown program as normal.

Keep in mind that now when you are connected to the Internet this way, your computer is a server. Although it is only a temporary connection with a changing IP address, you should start keeping server security issues in mind, but such issues are beyond the scope of this page.

Comments, found bugs, improvements, etc. can be sent to me at dougklim@provide.net.

egovision would like to express their appreciation to Doug Klimesh for allowing us to mirror this article. The original can be found at http://www.provide.net/~dougklim/DUNconf.html.

   
egovision


Articles and essays
As part of our on-going commitment to education and intellectual rigour, egovision are proud to make available a collection of written articles loosely related to the Web and the Internet in general.
Some of these essays will be the product of our own well worn keyboards, others are pieces made available to us, or submitted by those of you who share our interests.

If you have an article you feel would suit this resource, or even have an idea for an article please provide details via our response form and we will contact you as soon as possible.