Perl Net::SMTP fails on Fedora - perl

I have the following code which works fine with an AWS instance with Ubuntu but not with Fedora.
#!/bin/perl
use Net::FTP;
use Net::SMTP;
use warnings;
use POSIX qw(strftime);
my $now_string = localtime;
my $date = strftime "%d/%d/%Y", localtime;
my $checkdate = strftime "%d/%b/%Y", localtime;
my $username = "apikey";
my $passwd = "REDACTED";
my $from = 'Reports <reports#redacted.com';
my $smtphost = 'smtp.sendgrid.net';
my $smtp = Net::SMTP->new($smtphost, Port => 587, Debug => 1);
$smtp->auth($username,$passwd) || die "Authentication failed\n";
The output on the Fedora instance is:
Net::SMTP>>> Net::SMTP(3.11)
Net::SMTP>>> Net::Cmd(3.11)
Net::SMTP>>> Exporter(5.74)
Net::SMTP>>> IO::Socket::IP(0.39)
Net::SMTP>>> IO::Socket(1.40)
Net::SMTP>>> IO::Handle(1.40)
Net::SMTP=GLOB(0x564c8ac970b0)<<< 220 SG ESMTP service ready at ismtpd0027p1las1.sendgrid.net
Net::SMTP=GLOB(0x564c8ac970b0)>>> EHLO localhost.localdomain
Net::SMTP=GLOB(0x564c8ac970b0)<<< 250-smtp.sendgrid.net
Net::SMTP=GLOB(0x564c8ac970b0)<<< 250-8BITMIME
Net::SMTP=GLOB(0x564c8ac970b0)<<< 250-PIPELINING
Net::SMTP=GLOB(0x564c8ac970b0)<<< 250-SIZE 31457280
Net::SMTP=GLOB(0x564c8ac970b0)<<< 250-STARTTLS
Net::SMTP=GLOB(0x564c8ac970b0)<<< 250-AUTH PLAIN LOGIN
Net::SMTP=GLOB(0x564c8ac970b0)<<< 250 AUTH=PLAIN LOGIN
Authentication failed
The code is exactly the same on both instances but with the Fedora instance Authentication consistently fails.
Manually connecting to sendgrid via the command line also works.
Thank you in advance!

Related

Perl Script to set snmp setting on a router

I want to telnet to router and set snmp setting ( automated rather than typing it manually)
This is what I do manually:
[asafaaya#util-server-1 ~]$ telnet 10.222.24.152
Trying 10.222.24.152...
Connected to 10.222.24.152.
Escape character is '^]'.
Account:admin
Password: *********
Type ? for command help
>mngt snmp -e 1 -g OPTmr9 -s OPTmr9 -t OPTmr9
I tried to automate the above but the script is not working as expected. Appreciated if you could check the code below.
#!/usr/bin/perl
use strict;
use warnings;
use Net::Telnet;
use Regexp::Common qw/ net number /;
use Data::Dumper;
my $username = 'admin';
my $password = 'TuR!!!xxx';
my $router = $ARGV[0];
print Dumper $router;
my $t = new Net::Telnet (Timeout=>10);
$t->open($router);
$t->prompt('/router login:/i');
$t->print($username);
print Dumper $username, "\n";
$t->prompt('/Password:/i');
$t->print($password);
print Dumper $password, "\n";
$t->prompt('/^> /');
$t->cmd("mngt snmp -e 1 -g OPTmr9 -s OPTmr9 -t OPTmr9");
$t->print('exit');

Expect.pm to login to a router and execute a command

I'm trying to login to the router and execute a command. I'm able to do it with Net::ssh::Perl. But I'd like to use perl Expect.pm instead of Net::ssh::Perl. Can some one tell me how to do the following steps with expect.pm ?
use strict;
use warnings;
use Net::SSH::Perl;
my $host = "ip";
my $password = "password";
my $user = "user";
$ssh = Net::SSH::Perl->new($host);
$ssh->login($user, $password);
# Get into config terminal prompt
$ssh->cmd("configure terminal");
# Execute the command
my $cmd = $ssh->cmd("show crytpo session");
print($cmd);

PHPMAILER mail sent but not received

I am trying to use PHPMailer but I'm having problem. This is my test code:
<?php
require './PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 4;
$mail->Debugoutput = 'html';
$mail->Host = "localhost";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = "donotreply#myservermail";
$mail->Password = "mypassword";
$mail->setFrom('donotreply#myservermail', 'Mail test');
$mail->addReplyTo('myemail#live.com', 'First Last');
$mail->addAddress('donotreply#myservermail', 'Test mail');
$mail->Subject = 'PHPMailer SMTP test';
$mail->msg = 'Hello there this is a test . ';
$mail->Body = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
And here is the result from browser, and I see no error there:
Connection: opening to localhost:25, timeout=300, options=array ()
Connection: opened
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "220 localhost ESMTP"
SMTP -> get_lines(): $data is "220 localhost ESMTP"
SERVER -> CLIENT: 220 localhost ESMTP
CLIENT -> SERVER: EHLO MY_SERVER_NAME
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "250-localhost"
SMTP -> get_lines(): $data is "250-localhost"
SMTP -> get_lines(): $data was "250-localhost"
SMTP -> get_lines(): $str is "250-SIZE 20480000"
SMTP -> get_lines(): $data is "250-localhost250-SIZE 20480000"
SMTP -> get_lines(): $data was "250-localhost250-SIZE 20480000"
SMTP -> get_lines(): $str is "250-AUTH LOGIN"
SMTP -> get_lines(): $data is "250-localhost250-SIZE 20480000250-AUTH LOGIN"
SMTP -> get_lines(): $data was "250-localhost250-SIZE 20480000250-AUTH LOGIN"
SMTP -> get_lines(): $str is "250 HELP"
SMTP -> get_lines(): $data is "250-localhost250-SIZE 20480000250-AUTH LOGIN250 HELP"
SERVER -> CLIENT: 250-localhost250-SIZE 20480000250-AUTH LOGIN250 HELP
Auth method requested: UNKNOWN
Auth methods available on the server: LOGIN
Auth method selected: LOGIN
CLIENT -> SERVER: AUTH LOGIN
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "334 SOME_ENCRYPTED_STRING"
SMTP -> get_lines(): $data is "334 SOME_ENCRYPTED_STRING"
SERVER -> CLIENT: 334 SOME_ENCRYPTED_STRING
CLIENT -> SERVER: SOME_ENCRYPTED_STRING
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "334 SOME_ENCRYPTED_STRING"
SMTP -> get_lines(): $data is "334 SOME_ENCRYPTED_STRING"
SERVER -> CLIENT: 334 SOME_ENCRYPTED_STRING
CLIENT -> SERVER: SOME_ENCRYPTED_STRING
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "235 authenticated."
SMTP -> get_lines(): $data is "235 authenticated."
SERVER -> CLIENT: 235 authenticated.
CLIENT -> SERVER: MAIL FROM:<donotreply#MY_SERVER_MAIL>
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "250 OK"
SMTP -> get_lines(): $data is "250 OK"
SERVER -> CLIENT: 250 OK
CLIENT -> SERVER: RCPT TO:<MY_EMAIL#yahoo.com>
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "250 OK"
SMTP -> get_lines(): $data is "250 OK"
SERVER -> CLIENT: 250 OK
CLIENT -> SERVER: DATA
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "354 OK, send."
SMTP -> get_lines(): $data is "354 OK, send."
SERVER -> CLIENT: 354 OK, send.
CLIENT -> SERVER: Date: Thu, 14 May 2015 10:40:00 +0200
CLIENT -> SERVER: To: MY_EMAIL yahoo <MY_EMAIL#yahoo.com>
CLIENT -> SERVER: From: MAIL_SERVER test <donotreply#MY_SERVER_MAIL>
CLIENT -> SERVER: Reply-To: MY_EMAIL Live <MY_EMAIL#live.com>
CLIENT -> SERVER: Subject: PHPMailer SMTP test
CLIENT -> SERVER: Message-ID: <SOME_ENCRYPED_STRING#MY_SERVER_NAME>
CLIENT -> SERVER: X-Priority: 3
CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.10 (https://github.com/PHPMailer/PHPMailer/)
CLIENT -> SERVER: MIME-Version: 1.0
CLIENT -> SERVER: Content-Type: text/plain; charset=iso-8859-1
CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
CLIENT -> SERVER:
CLIENT -> SERVER: This is a plain-text message body
CLIENT -> SERVER:
CLIENT -> SERVER: .
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "250 Queued (0.000 seconds)"
SMTP -> get_lines(): $data is "250 Queued (0.000 seconds)"
SERVER -> CLIENT: 250 Queued (0.000 seconds)
CLIENT -> SERVER: QUIT
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "221 goodbye"
SMTP -> get_lines(): $data is "221 goodbye"
SERVER -> CLIENT: 221 goodbye
Connection: closed
Message sent!
I've tried this test with different emails, on yahoo, gmail, etc, but still the same. I also checked carefully the spam for all my emails, for couple hours.
Any idea?
It is succeeding - in delivering to localhost, as you're asking it to. What happens after that is entirely outside PHPMailer's control. You need to look at your local mail server's log to see what happens after that, but it looks like it's not getting any further than that, so you need to look at your mail server's config.

I'm sending a test email using perl but it comes back with system can't find path specified

I've tried:
#!/usr/bin/perl
$to = 'abcd#gmail.com';
$from = 'webmaster#yourdomain.com';
$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';
open(MAIL, "|/usr/sbin/sendmail -t");
# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
# Email Body
print MAIL $message;
close(MAIL);
print "Email Sent Successfully\n"
but it comes back with The system cannot find the path specified.
I've also tried:
#!/usr/bin/perl
use MIME::Lite;
$to = 'abcd#gmail.com';
$cc = 'efgh#mail.com';
$from = 'webmaster#yourdomain.com';
$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';
$msg = MIME::Lite->new(
From => $from,
To => $to,
Cc => $cc,
Subject => $subject,
Data => $message
);
$msg->send;
print "Email Sent Successfully\n";
but it comes back with SMTP Failed to connect to mail server: No such file or directory at G:\email_test.pl line 18
How do I fix this/these issues so I can send an email successfully? These seem to be the two common examples to use when sending email using PERL and I can't get them to work.
If you're getting G:\email_test.pl, I am assuming you're using a Windows machine. I see open(MAIL, "|/usr/sbin/sendmail -t"); in your program which refers to a program on a Unix/Linux system.
In Perl, the Net::SMTP module comes with almost all Perl distributions. I highly recommend people to use that unless they need to MIME encode mail.
The module is pretty simple to use too although admittedly not as simple as other email modules. Net::SMTP assumes you know how SMTP works. Fortunately, the S in SMTP stands for Simple. Of course, the people who called it Simple is the same group that thought Emacs was an intuitive program editor.
# /usr/bin/env perl
use strict;
use warnings;
use feature qw(say);
use constant {
SMTP_HOST => 'mailhost',
TO => 'abcd#gmail.com',
FROM => 'efgh#mail.com',
SUBJECT => 'Test Email',
USER => 'question_guy',
PASSWORD => 'swordfish,
};
my $smtp = Net::SMTP->new( SMTP_HOST ) # This is your SMTP host
or die qq(Cannot create Net::SMTP Object);
$smtp->auth( USER, PASSWORD ) # If you have to use authentication
or die qq(Can't authenticate into ) . SMTP_HOST;
$smtp->mail( FROM );
$smtp->to( TO );
my $message = "Subject: " . SUBJECT . "\n"
. "To: " . TO . "\n"
. 'This is test email sent by Perl Script';
$smtp->data;
$smtp->datasend( $message );
$smtp->dataend;
$smtp->quit;
This above wasn't tested, but I was typing it from a program I wrote that used Net::SMTP. Take a look at the Net::SMTP documentation and play around with it.

Unknown reason for Known error in the perl script

I am writing following script to read list of servers from the text file them ssh to them and run ldd command to fetch the version that is installed on the server.
The only problem is the error that I am seeing following error which says Bad Host name:
adev#abclnxdev:[/home/adev/perl-scripts] {63} % perl try.pl
Net::SSH: Bad host name: abclnxtest01
at try.pl line 21
when I manually do the ssh to this host. It gets connect.
Here is script :
#!/mu/bin/perl
use Net::SSH::Perl;
use warnings;
my $file = "server-list.txt";
my $usr = "user";
my $pwd = "password";
my $output_file = "GlibC-version.txt";
open(HANDLE, $file) or die("Cant open the file :( ");
#server_list = <HANDLE>;
close(HANDLE);
#debug_print_array(#server_list);
open(HANDLE, ">>$output_file"); #opening file for output.
foreach $host (#server_list)
{
my $ssh = Net::SSH::Perl->new($host);
$ssh->login($usr,$pwd,$ssh);
my($stdout, $stderr, $exit) = $ssh->cmd("ldd --version|grep ldd");
print HANDLE "----------------------------------";
print HANDLE "Hostname : $host";
print HANDLE "GLIBC Version : $stdout";
print HANDLE "----------------------------------\n\n";
}
You have a newline at the end of the server name.
Add:
chomp #server_list;
(And incidentally, it's better to use the newer 3-argument open(); see http://perlmaven.com/open-files-in-the-old-way .)