#!/usr/bin/perl -w # use strict; use Getopt::Long; #;;;;;;;;;;;; # tested on solaris 7 and 8 # change ip and hostname with submitted arguments from cmd # # change router ip as well if new ip is on different subnet #;;;;;;;;;;;; my ($opt_newip,$opt_oldip,$opt_newhost,$opt_oldhost); my @ipFiles = qw(/etc/hosts); my @routerFiles = qw(/etc/defaultrouter); my @hostFiles = qw(/etc/hosts /etc/nodename /etc/hostname.hme0 /etc/net/ticlts/hosts /etc/net/ticots/hosts /etc/net/ticotsord/hosts ); GetOptions( 'newip=s' => \$opt_newip, 'oldip=s' => \$opt_oldip, 'newhost=s' => \$opt_newhost, 'oldhost=s' => \$opt_oldhost, ); # none of the arguemnts supplied if (!$opt_newip && !$opt_oldip && !$opt_newhost && !$opt_oldhost) { printHelp(); exit; } # catch the case that the arguments are not supplied by pair if ( ($opt_newip xor $opt_oldip) || ($opt_newhost xor $opt_oldhost) ) { print "Error: need pair of arguemnts\n"; printHelp(); exit; } if ($opt_newip && $opt_oldip) { doReplace($opt_newip,$opt_oldip,\@ipFiles); # extract 1.2.3. from 1.2.3.4 # comparing extracted subnets, # change router ip if subnets are different if ( (my ($subNew)=$opt_newip=~/(.*)\.\d+/) && (my ($subOld)=$opt_oldip=~/(.*)\.\d+/) ) { doReplace($subNew,$subOld,\@routerFiles) unless ($subNew eq $subOld); } } if ($opt_newhost && $opt_oldhost) { doReplace($opt_newhost,$opt_oldhost,\@hostFiles); } sub doReplace { my ($new,$old,$files) = @_; my $regex="s/\Q\b$old\E/$new/g"; foreach my $f (@$files) { my $cmd = "perl -i -pe '".$regex."' $f"; print "cmd: $cmd\n"; #print "\t $f changed\n" unless (system($cmd)); } } sub printHelp { print "Change hostname and/ip in all config files for Solaris\n"; print "Change router ip in config files as well base on supplied IPs\n"; print "eg: script -oldip [ip] -newip [ip] -oldhost [host] -newhost [host]\n"; }