I have 2 files one of them has all the all mac addresses and the other one has all the ip addresses. Basically, I want to loop thru those 2 files and generate a configuration like below:
Asuming that you are sure that you have the same number of lines on both files, and every record is on a single line, you can use the following script:
#!/usr/bin/perl -w
#
open(MACS,"mac.txt") or die("Could not open mac.txt");
open(IPS,"ip.txt") or die("Could not open ip.txt");
open(RESULT,">result.txt") or die("Could not open result.txt");
my @macs = ();
my @ips = ();
push(@macs,$_) while( <MACS> );
push(@ips,$_) while( <IPS> );
chomp(@macs);
chomp(@ips);
for( $i=0 ; $i<=$#macs ; $i++ ){
print RESULT "host www$i.domain.com {\n";
print RESULT "hardware ethernet $macs[$i];\n";
print RESULT "fixed-address $ips[$i];\n";
print RESULT "}\n\n";
}
close(MACS);
close(IPS);
close(RESULT);
The hostname is actually the last octet of the ip. I ended up creating a separate hostname file and was able to get the final result with Sergiu-IT's script.