conditional email


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting conditional email
# 1  
Old 03-05-2008
Bug conditional email

Some one please help me: I need to perl code for a conditional block:

I believe I need to put the $email->send; command in a conditional block that checks if $duplicate_ip or $duplicate_host is defined, and then send the email. How can I do that:

use strict;
use warnings;
use MIME::Lite;
my (%ip, %host, $duplicate_ip, $duplicate_host, @linevals, $outline);
my $host_file = '/etc/hosts';
open my $file, '<', $host_file or die "can't open $host_file $!";
while (<$file>) {
chop;
if( my ($ip, $host) = /^#?([\d.]+)\s+(\S+)/ ) {
@linevals = split;
$outline = $linevals[0] . "\t" . $linevals[1] . "\n";
push @{$ip{$ip}}, $outline;
push @{$host{$host}}, $outline;
}
}
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 ( sort keys %host ) {
if ( @{$host{$host}} > 1 ) {
my ($ip) = $host{$host}[0] =~ /^#?([\d.]+)/;
unless ( @{$ip{$ip}} > 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 => 'xxx@xxx.com',
To => 'xxxx@xxx.com',
#Cc => 'xxx@xx.com,xxxx@att.com',
Subject => 'Host file duplicates',
Data => $email_msg
);
$email->send
# 2  
Old 03-05-2008
The code's too hard to make out with the CODE tages around it but it sounds like you need this:
Code:
if ($duplicate_ip || $duplicate_host) {
  stuff
}

# 3  
Old 03-05-2008
Power

Is this code goes after $email->send:

if ($duplicate_ip || $duplicate_host) then
$email->send

bottom line is e-mail will be send if duplicate is found otherwise don't send an email.

thanks

Last edited by ricky007; 03-06-2008 at 12:18 AM..
# 4  
Old 03-06-2008
Quote:
Originally Posted by ricky007
Is this code goes after $email->send:

if ($duplicate_ip || $duplicate_host) then
$email->send

bottom line is e-mail will be send if duplicate is found otherwise don't send an email.
Looks about right, although perl uses { and } rather than 'then'
And yep, if either of the two duplicate_xxx variables have something set in them it'll send the email, if both of them are empty it won't do anything.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sending sql output to email body with conditional subject line

hi , i have written below piece of code to meet the requirement but i am stuck in the logic here. the requirement are: 1) to send the sql out put to email body with proper formatting. 2) if count_matching = Yes then mail should triggered with the subject line ... (10 Replies)
Discussion started by: itzkashi
10 Replies

2. Shell Programming and Scripting

Getting email output in single line with out space in email

I have tried below email method and i am getting every thing in single line . i have put echo to provide space, but it is not helping my code ( echo "From: $FROM" echo "To: $MAILTO" echo "CC: $CC" echo "Subject: $SUBJECT" echo "MIME-Version: 1.0" echo 'Content-Type: multipart/mixed;... (6 Replies)
Discussion started by: mirwasim
6 Replies

3. SuSE

Send outgoing email to my GroupWise email

Dear users, I have Linux server whose versions are Suse 10 SP 3 and Suse 11. I am trying to send email from these servers to my GroupWise email account. In /etc/postfix/main.cf file, The current value of MYHOSTNAME is LINUX.LOCAL. What should be the right value of MYHOSTNAME? Is... (0 Replies)
Discussion started by: JDBA
0 Replies

4. Shell Programming and Scripting

Using top command to email if process is exceeding 25% and sending an email alert if so

This is my first time writing a script and Im having some trouble, Im trying to use the top command to monitor processes and the amount of CPU usage they require, my aim is to get an email if a process takes over a certain percentage of CPU usage I tried grep Obviosly that hasnt worked, Any... (8 Replies)
Discussion started by: jay02
8 Replies

5. UNIX for Dummies Questions & Answers

new to ldap, send email to a ou or group, and see a list from email client

hi, i'm running openldap on ubuntu 10.04, creating new items with apache directory studio (windows version). i use the ldap just as an address book to our small office (email clients are windows live mail 2009, 2011, microsoft outlook 2007 and 2010). a. i cant see a list of the contacts,... (0 Replies)
Discussion started by: V4705
0 Replies

6. Solaris

Send an email from Solaris using Linux email server

Hello everyone I have a problem and I need your help: I have a Solaris 10 and Solaris 8 UNIX Servers, and Linux Centos4 as email server. I need send an email from Solaris servers preferably using Centos4 email server. I have no mail service configured in my Solaris computers (1 Reply)
Discussion started by: aflores
1 Replies

7. UNIX for Dummies Questions & Answers

Send email where # is in the email address - Using Unix

Hi All, How do I send an email using malix where email address contains a #. I have a email address like this : #test@test.com I want to send email like malix -s "TEST" #test@test.com < SOMEFILE I tried \# but doesn't work. Please let me know how we can achieve this? I am in... (1 Reply)
Discussion started by: jingi1234
1 Replies

8. UNIX for Advanced & Expert Users

Unable to send eMail from a UNIX-Host ( using mailx ) to a Outlook-email-addres(Win)

Hi A) I am able to send eMail using mailx from a UNIX ( solaris 8 ) host to my Outlook-email-ID : FName.Surname@Citigroup.com ( This is NOT my actual -eMail-ID). But in Outlook the "From :" eMail address is displayed as " usr1@unix-host1.unregistered.email.citicorp.com " .i.e the words... (2 Replies)
Discussion started by: Vetrivela
2 Replies
Login or Register to Ask a Question