my perl script won't send any mails

Need help with C, C++, perl, python, etc?

my perl script won't send any mails

Postby cc » 2006-11-10 16:04

hi

I have this perl script, but it won't send any mails:
Code: Select all
#!/usr/bin/perl -w

use strict;
use warnings;

my $PingHost = "10.10.0.12";
my $ExpectedRedirect = "10.10.0.10";
my $mailto = "admin\@mydomain.net";

my $ip = 'ip route flush cache';

open( INPING, "ping -c 4 $PingHost|" ) || die "ping open failed";
while( my $line = <INPING> ) {
  next unless( $line =~ /Redirect Host\(New nexthop: (.*)\)/ );
  next if( $1 eq $ExpectedRedirect );

   open( OUTMAIL, "| mailx -s 'CH Branch VPN Unexpected Redirect: $1' $mailto" ) || die "pipe to mail failed";
   print OUTMAIL scalar localtime();
   print OUTMAIL "\n\n";
   print OUTMAIL "Received an unexpected redirect \n";
   print OUTMAIL "\n";
   print OUTMAIL "have a nice day\n";   
}
close(INPING) || warn "bad pipe close";

exit;

knows someone what's wrong and howto solve this problem ?
cc
 
Posts: 820
Joined: 2005-06-08 19:14

Postby kink » 2006-11-11 11:58

From a cursory look my best guess is that your regular expression does not match and therefore no mail is sent. You could try to deduce where it fails by adding some debug statements.
User avatar
kink
Debian Developer, Site Admin
Debian Developer, Site Admin
 
Posts: 287
Joined: 2006-01-02 16:47
Location: Utrecht, The Netherlands

Postby drl » 2006-11-11 14:36

Hi.

I checked that my mailx would send messages, then I created the stripped-down version of your script as noted below and it worked for me.

You could remove the test and see if the print will work. You could also make the print go to STDOUT if you suspect the mailx-pipe chain ... cheers, drl
Code: Select all
#!/usr/bin/perl

# @(#) p1       Demonstrate mail from perl script.

use strict;
use warnings;
use Carp;

my ($mail);
my ($program)   = "mailx";
my ($recipient) = "drl";
my ($domain)    = '@leap';

open $mail, "|-", "$program -s test $recipient$domain"
    || croak("Can't fork $program, error $!");

print $mail scalar localtime(), "\n";
print $mail "hello, world.\n";

close $mail || croak("Can't close $program: error $!");

exit(0);
["Sure, I can help you with that." -- USBank voice recognition system.
( Mn, 2.6.x, AMD-64 3000+, ASUS A8V Deluxe, 3 GB, SATA + IDE, NV34 )
Debian Wiki | Packages | Backports | Netinstall
User avatar
drl
 
Posts: 434
Joined: 2006-09-20 02:27
Location: Saint Paul, Minnesota, USA

Postby arnestig » 2006-11-21 09:50

example using sendmail:
Code: Select all
#!/usr/bin/perl
open (OUT,"|/usr/sbin/sendmail -F 'input_the_sender_name' -f sender_adress\@thedomain.com -t");
print(OUT "To: send_to_adress\@thedomain.com\n");
print(OUT "Subject: "Just an example"\n");
print(OUT "\n");
print(OUT "Test example"\n");
close(OUT);
User avatar
arnestig
 
Posts: 13
Joined: 2006-11-21 09:19
Location: Grästorp, Sweden


Return to Programming

Who is online

Users browsing this forum: No registered users and 7 guests

fashionable