#!/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"; }

In reply to Solaris - change hostname / ip / default-router-ip script by Qiang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.