I need a perl script, which will run every midnight via cronjob and e-mail few users once it finds any duplicated value in a file which is located /etc/hosts, the file name is called hosts and the format of the file has 3 colums and some time 2 columns. The script will look for duplicate IP or Duplicate device name, the script must not ignore any row start with "#". The file format <IP>TAB<DeviceName>TAB<Description>, I really dont care the third column. Here is an example of the file:
I have compiled the following works fine, but I am getting IP and device all together. I need to separate them, I need only the duplicate device name and then next paragraph duplicate IP only:
#!/opt/sa/bin/perl
use strict;
use warnings;
use MIME::Lite;
my (%ip, %host, $duplicates);
my $host_file = '/etc/hosts';
open my $file, '<', $host_file or die "can't open $host_file $!";
while (<$file>) {
if( my ($ip, $host) = /^#?([\d.]+)\s+(\S+)/ ) {
if ( defined $ip{$ip} or defined $ip{$host} ) {
$duplicates .= $_;
}
else {
$ip{$ip}++;
$ip{$host}++;
}
}
}
close $file;
my $email_msg = <<EMAIL_MSG;
The following entries in the host file are dulpicates
either by IP address or by hostname.
$duplicates
EMAIL_MSG
my $email = MIME::Lite->new(
From => 'xx@xx.com',
To => 'xv@xv.com',
Cc => '33@xx.com,42@xx.com',
Subject => 'Host file duplicates',
Data => $email_msg
);
$email->send;
the output went in both section "Duplicate IP" and "Deuplicate hosts" instead going one of the section.
Output not coming up with TAB in between the IP<>DeviceName<Restoftheinfo>
use strict;
use warnings;
use MIME::Lite;
my (%ip, %host, $duplicate_ip, $duplicate_host);
my $host_file = '/etc/hosts';
open my $file, '<', $host_file or die "can't open $host_file $!";
while (<$file>) {
if( my ($ip, $host) = /^#?([\d.]+)\s+(\S+)/ ) {
push @{$ip{$ip}}, $;
push @{$host{$host}}, $;
}
}
close $file; #print "Duplicate IP's with hostnames\n";
foreach my $ip ( keys %ip ) {
if ( @{$ip{$ip}} > 1 ) {
$duplicate_ip .= join ('', @{$ip{$ip}}) . "\n\n";
}
} #print "\nDuplicate hostnames with IP's\n";
foreach my $host ( keys %host ) {
if ( @{$host{$host}} > 1 ) {
$duplicate_host .= join ('', @{$host{$host}}) . "\n\n";
}
}
my $email_msg = <<EMAIL_MSG;
The following entries in the host file are duplicates
either by IP address or by hostname.
Duplicate IP addresses:
$duplicate_ip
Duplicate Hostnames:
$duplicate_host
EMAIL_MSG
print $email_msg;
my $email = MIME::Lite->new(
From => 'xxxx@xx.com',
To => 'xxxx@xx.com', #Cc => 'yyyy@yy.com,xxzzz@zz.com',
Subject => 'Host file duplicates',
Data => $email_msg
);
$email->send