[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] Changing IP



Naked art -- here is what happens when you (OK, I) have 10mins to
write a script before the plug gets pulled and this router is shipped
to its next destination.

Note: *still* use strict & warnings :-)

Paul

#!/usr/bin/perl -w
use strict;

(my $prog = $0) =~ s~^.*/~~;
die "$prog new_ip new_mask new_gw\nExample: $prog 1.2.3.4 255.0.0.0 1.2.3.1\n"
	unless @ARGV == 3;

my ($new_ip, $new_mask, $new_gw) = @ARGV;
my $current_ip = '63.196.114.230';
my $current_gw = '63.196.114.1';
my $current_mask = '255.255.255.0';

my @config_files = map { "/etc/$_" } qw(
	hosts network/interfaces
	apache/httpd.conf dhcpd.conf bind/named.conf rsyncd.conf
	lrp-firewall/ipfilter.conf lrp-firewall/network.conf
);
push @config_files, $0;	# self-modifying!

for my $file (@config_files) {
	print "Modifying '$file'...\n";
	my $sys = qq[
		perl -pi~ -e '
			s/\\b$current_ip\\b/$new_ip/g;
			s/\\b$current_gw\\b/$new_gw/g;
			s/\\b$current_mask\\b/$new_mask/g;
	' $file];
	system($sys) == 0 or die "$prog: $sys failed: $@";
	# print "Just did:\n$sys\n";
}

print <<DONE;
Done.

Reboot for changes to take effect or restart the network, Apache web
and BIND name services:

# /etc/init.d/networking restart
# ndc restart
# apachectl graceful
DONE

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe