| Category: | Utility Scripts |
| Author/Contact Info | |
| Description: | I got bored when I had to change the ip/hostname from time to time on solaris 7 or 8 machines, there are too many files need to be changed! you can change ip or hostname, or do both the same time. If the new ip is on different subnet from the old one. default router ip gets changed too (like the second example). two examples of running this script. currently this script only prints out the command it is going to perform. To use it, comment out the following line in the script.
I wish i could make this script shorter :) UPDATE: adds \Q \E and \b also, don't trust user input (although in this case only myself) and validate it before processing. |
#!/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 e
+q $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 I
+Ps\n";
print "eg: script -oldip [ip] -newip [ip] -oldhost [host] -newhost
+ [host]\n";
}
|
|
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Solaris - change hostname / ip / default-router-ip script
by graff (Chancellor) on Feb 20, 2005 at 23:48 UTC | |
by Qiang (Friar) on Feb 21, 2005 at 03:31 UTC | |
by graff (Chancellor) on Feb 21, 2005 at 15:07 UTC | |
|
Re: Solaris - change hostname / ip / default-router-ip script
by Taulmarill (Deacon) on Feb 21, 2005 at 09:25 UTC | |
by Qiang (Friar) on Feb 22, 2005 at 02:20 UTC | |
by Taulmarill (Deacon) on Feb 26, 2005 at 12:36 UTC | |
by Anonymous Monk on Feb 27, 2005 at 04:51 UTC | |
by Taulmarill (Deacon) on Mar 01, 2005 at 11:11 UTC |