You can use the following perl script exchange_ips.pl to exchange the ip either by three constant ips (function exchange_const()) or by three incremental ips (function exchange_incr()). Just add a comment (#) in front of the function call at the end of the script that you don't need.
Note that one or more incremental ips will be incorrect if the original ip ends with 253 or higher.
The script modifies all lines starting with $domainname. If the domain name doesn't matter or if your files only contain lines for just one/the same domain, then set my $domainname = ".+";.
#!/usr/bin/perl
use strict;
use warnings;
my @data = <STDIN>;
my $domainname = "subdomain";
my $const_ip1 = "123.123.123.123";
my $const_ip2 = "124.124.124.124";
my $const_ip3 = "125.125.125.125";
my $pattern = qr/^($domainname\s+A\s+)([\d\.]+)/is;
sub exchange_const($) {
my $dataref = shift;
my $found = 0;
foreach (@$dataref) {
if ($_ =~ m/$pattern/) {
unless ($found) {
$found = 1;
printf "%s%s\n%s%s\n%s%s\n", $1, $const_ip1, $1, $const_ip2, $1, $const_ip3;
}
} else {
print $_;
}
}
}
sub exchange_incr($) {
my $dataref = shift;
my $found = 0;
foreach (@$dataref) {
if ($_ =~ m/$pattern/) {
unless ($found) {
$found = 1;
my $const = $1;
my @iparr = split /\./, $2;
for (1 .. 3) {
$iparr[3]++;
printf "%s%s\n", $const, join ".", @iparr;
}
}
} else {
print $_;
}
}
}
# exchange ip with constant ips
exchange_const(\@data);
# exchange ip with incremental ips
exchange_incr(\@data);
Call the script like this:
perl exchange_ips.pl < your_file
Loop over all files (with backup):
for f in /named_ext/*; do rename s/$/.bak/ $f; perl exchange_ips.pl < $f.bak > $f; done
Example:
Sample file to modifiy:
# some foo
subdomain A 200.201.202.203
# duplicate
subdomain A 200.201.202.203
# some bar
otherdomain A 99.99.99.99
otherdomain A 1.1.1.1
# another duplicate
subdomain A 200.201.202.203
Output of exchange_const():
# some foo
subdomain A 123.123.123.123
subdomain A 124.124.124.124
subdomain A 125.125.125.125
# duplicate
# some bar
otherdomain A 99.99.99.99
otherdomain A 1.1.1.1
# another duplicate
Output of exchange_incr():
# some foo
subdomain A 200.201.202.204
subdomain A 200.201.202.205
subdomain A 200.201.202.206
# duplicate
# some bar
otherdomain A 99.99.99.99
otherdomain A 1.1.1.1
# another duplicate
* A 200.201.202.203- in other words, any subdomain name, but specific A and IP address, or is the subdomain the same in each file? – Paul Aug 13 '12 at 7:14