Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Updating config files with dynamic ip

by gornox_zx (Priest)
on Jul 27, 2001 at 17:33 UTC ( #100294=perlquestion: print w/replies, xml ) Need Help??
gornox_zx has asked for the wisdom of the Perl Monks concerning the following question:

My situation is this:
I have cable internet service provided through rcn. Recently they started frowning upon my staticly set ip number, and kept disconnecting my service for doing so. Hence, I decided to do as they wished and use a dynamic ip. The problem then arose that my apache vhost configuration file, and several cgi configuration files needed to be updated every time my ip changed so that they would work. Thus I wrote this script to update my /etc/httpd/conf/virtualhosts.conf file. I also made this script write the file /usr/share/global.ip so that if I had any other scripts that needed my ip i could just put in require "/usr/share/global.ip"; and the put $ipnum everywhere I needed my ip. This all worked.

The problem is this: Some of the configuration files aren't executable so I can't use the global.ip file with them, and also I can't/don't want to imbed long files into the script as I did with the vhost configuration file. Does anyone have any suggestions/comments on how I could improve my system, and more importantly get these text configuration files updated. The following is the code for my script:
#!/usr/bin/perl -w use strict; # a good idea; it brings into existence three strictures my $rawbuf; my $ipbuf; $rawbuf=`/sbin/ifconfig eth0`; if( $rawbuf =~ /(\d+\.\d+\.\d+\.\d+)/ ) { $ipbuf= $1; } else { print "You have no ip address you dazzle-duck! \n"; } vhost($ipbuf); globalip($ipbuf); #-----------------------Behold the function arena--------------------- +# #This function is the template for the apache vhost configuration file +; listen to it sing sub vhost{ my $vfile = "/etc/httpd/conf/virtualhosts.conf"; open (VHOST, "> $vfile") or die "The virtual hosts configuration file +is virtual all right! $!\n"; my $ipnum = "$_[0]"; my @bigbuf = ( "NameVirtualHost $ipnum\n\n", "<VirtualHost $ipnum>\n", "User hg\n","Group hg\n", "DocumentRoot /www/hg\n", "ServerName 0tis.org\n", "ServerAlias www.0tis.org\n", "ScriptAlias /cgi-bin /www/hg/cgi-bin\n", "</VirtualHost>\n" ); while (@bigbuf){ my $printbuf = shift ( @bigbuf ); print VHOST $printbuf; } } #code for global ip template sub globalip{ my $ipnum = "$_[0]"; my $globalip = "/usr/share/global.ip"; open (GIP,"> $globalip") or die "You silly silly man, why must you ins +ist on opening a non-existant configuration file $!\n"; print GIP "\$ipnum = $ipnum;\n1;"; close (GIP); }
~heise2k~

Replies are listed 'Best First'.
Re: Updating config files with dynamic ip
by mr.nick (Chaplain) on Jul 27, 2001 at 17:56 UTC
    Well, first of all I would suggest that you upgrade your version of Apache. Newer versions (>= 1.3.19) allow for wildcards in your httpd.conf file. Here is a snippet of mine:
    NameVirtualHost * <VirtualHost *> DocumentRoot /web/htdocs/mrnick ServerName www.mrnick.binary9.net CustomLog logs/mrnick.log combined CustomLog logs/access.log mine </VirtualHost> <VirtualHost *> DocumentRoot /web/htdocs/radio CustomLog logs/radio.log combined Servername ourthing.radio.binary9.net CustomLog logs/access.log mine </VirtualHost>
    Notice how no IP addresses are directly coded into the file? This would allow you NOT to have to update it everytime you change IP's. You might have to apachectl restart it after it has changed, though. If you are using dhcpc, there is a file (/etc/dhcpc/dhcpcd-eth0.exe) that is run when your IP address changes. Just restart your server from in there.

    mr.nick ...

(ar0n: <Perl> sections) Re: Updating config files with dynamic ip
by ar0n (Priest) on Jul 27, 2001 at 19:35 UTC
    If you have mod_perl compiled, you can use <Perl> sections to dynamically configure your apache configuration. This is what jcwren uses to automatically configure a virtual host for each of his users. He simply adds to /etc/perlmonk_users, and the next time apache starts up, the virtual host is automatically configured:
    <Perl> my $server = "perlmonk.org"; my @users = do { local @ARGV = "/etc/perlmonk_users"; <> }; chomp @u +sers; foreach my $user (@users) { push @{ $VirtualHost{"*"} } , { ServerName => "$user.$server", ServerAdmin => "$user\@$server", DocumentRoot => "/home/$user/public_html", ScriptAlias => [ "/cgi-bin/", "/home/$user/public_html/cgi-bin/" + ], CustomLog => [ "/home/$user/www_logs/access_log", "common" ], ErrorLog => "/home/$user/www_logs/error_log" }; } </Perl>
    Anyway, it's just an example of what can be done. Space is the limit... (most likely; the physicists haven't worked that one out yet)

    ar0n ]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://100294]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2019-04-20 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I am most likely to install a new module from CPAN if:
















    Results (110 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!