I think you might have a couple problems in the "doReplace" sub, because of how you build the replacement regex:
my $regex = "s/$old/$new/g";

On top of that, you're taking it for granted that the command line option strings really are valid IP addresses and host names, and you're putting them into a shell command that you then pass to a single-arg system() call. Personally, I'd feel better if these were checked first -- better still if you don't put the one-liner perl script on the command line at all.

In terms of simplifying things (while also fixing the problems above), you could consider something like this (untested):

#!/usr/bin/perl use strict; use Getopt::Long; # ... define arrays of file names, then ... my ( $opt_ip, $opt_host ); GetOptions( 'ip=s' => \$opt_ip, 'host=s' => \$opt_host ); my $Usage = "Usage: $0 [-ip old:new] [-host old:new]\n"; die $Usage unless ( $opt_ip =~ /\S:\S/ or $opt_host =~ /\S:\S/ ); my $ipreg = qr/\d{1,3}(?:\.\d{1,3}){3}/; my $hostreg = qr/\w+(?:\.\w+){2,}/; if ( $opt_ip ) { $opt_ip =~ /($ipreg):($ipreg)/ or die "Bad value for -ip: $opt_ip\ +n $Usage"; my ( $old, $new ) = ( $1, $2 ); doReplace( $old, $new, 'IP', \@ipFiles ); s/.\d+$// for ( $old, $new ); doReplace( $old, $new, 'RTR', \@routerfiles ) if ( $old ne $new ); } if ( $opt_host ) { $opt_host =~ /($hostreg):($hostreg)/ or die "Bad value for -host: +$opt_host\n $Usage"; my ( $old, $new ) = ( $1, $2 ); doReplace( $old, $new, 'HOST', \@hostFiles ); } sub doReplace { my ( $old, $new, $typ, $files ) = @_; open TMP,">/tmp/$typ-config-editor.$$.perl" or die "can't write sc +ript file: $!" print TMP "s{\\b\\Q$old\\E\\b}{$new}\n"; close TMP; for my $f ( @$files ) { my $cmd = "perl -i .bak.$$ -p /tmp/$typ-config-editor.$$.perl +$f"; print "cmd: $cmd\n"; # system( $cmd ) or print "\t $f changed\n"; # (system returns exit status of $cmd: 0 for success) } }
That still has the potential for trouble when editing the router files, if they contain any full IP addresses where the latter three components might match the first three components of the "old" IP address. If that's really a risk, you'll need to write a special one-liner script just for them.

In reply to Re: Solaris - change hostname / ip / default-router-ip script by graff
in thread 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.