This is a great program for all people running apache with dynamic IP - It gets your IP thru DOS's IPconfig (change that for the appropriate UNIX program), and places it in the httpd.conf overwriting your old IP, and starts apache. a few notes: A. This is made for my comp, as you'll see via the dirnames, etc. So: 1. Change the dir in the Open and System calls to reflect your dir. 2.Change the `ipconfig` to a UNIX console IP getting program and change the regex to reflect that. 3. BACK UP HTTPD.CONF! This is extremely important. 4. Check if your ServerName line in Httpd.conf is really number 256 (this is the default with an unchanged 1.3x Apache) UPDATE: Added a critical \n which will save your .conf file from getting funky. For all of those who've had their httpd.conf already ruined: echo $100 > /dev/null@yourhostname.net . Sorry.
#!/usr/bin/perl -w use strict; $_ = `ipconfig`; /(\d+\.\d+\.\d+\.\d+)\s*$/; my (@try); print "Starting apache service with $1 as IP...\n"; open(CONF, "C:\\progra~1\\apache~1\\apache\\conf\\httpd.conf") || die +"cannot open file: $!"; @try=<CONF>; close(CONF); print "$try[256] old IP"; $try[256]="ServerName $1\n"; #UPDATE HERE print "$try[256] is IP for apache"; open(CONF, ">C:\\progra~1\\apache~1\\apache\\conf\\httpd.conf") || die + "cannot open file3: $!"; foreach (@try) { print CONF "$_"; } close CONF; system("C:\\Progra~1\\Apache~1\\Apache\\Apache.exe") || die "Can't sta +rt apache service: $!";

Replies are listed 'Best First'.
(jeffa) Re: Apache IP-Autoupdater
by jeffa (Bishop) on Dec 06, 2000 at 23:26 UTC

    Relying on a paticular line to be at a particular position in a data file is a bad thing. Never trust hard-coded values.

    I was playing around and came up with this little snippet to find out what line the ServerName directive is located on in the httpd.conf file, as well as it's value:

    
    use strict;
    
    open (FILE, 'httpd.conf') or die "Painfully";
    my @a = <FILE>;
    close FILE;
    
    my $line_no;
    my $name;
    
    for (0..$#a) {
        if ($a$_ =~ /^ServerName\s+(.+)\s*$/) {
            $line_no = $_ + 1;
            $name = $1;
            last;
        }
    }
    
    print "Line = $line_no\nName = $name\n";
    
    
    I am sure this can be condensed elegantly, I tried it with grep, but couldn't get it to work. I am sure someone in the Monastery will improve this, especially my lazy use of .+ (if Dot Star is dead, can we still use Dot Plus?)

    Jeff

    
    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
    
      How about this?
      @ARGV = ('httpd.conf'); while (<>) { print("Line = $.\nName = $1\n"), last if /^ServerName\s+(\S+)\s*$/; }
      This could be reduced to a one-liner:
      % perl -ne 'print("Line=$.\nName=$1\n"), last if /^ServerName\s+(\ +S+)\s*$/;' httpd.conf
      Bit long though.
Re: Apache IP-Autoupdater
by KM (Priest) on Dec 06, 2000 at 23:51 UTC
    You can also take a look at some of the Apache::* modules like Apache::httpd_conf and Apache::PerlSections to get an idea of how to do some conf file stuff. Of course, those modules need mod_perl so YMMV.

    Cheers,
    KM

Re: Apache IP-Autoupdater
by Nimster (Sexton) on Dec 07, 2000 at 01:56 UTC
    Btw,

    on Jeffa's comment:
    I voted it up cause you practically did it. I thought of finding the line with a regex, isn't too hard. But, it was meant for my machine. It works on my machine. I suggest you all add jeffa's addition in the appropriate place.
    -Nimster