Cannot sysread (): EOF Error while connecting to Tibco JMS Topic using Net:: STOMP:: Client - perl

I am using the Net::STOMP::Client module to connect to a Tibco JMS Topic Server which is not hosted by me.
I am trying the SSL approach for which, I have a certificate.p12 file from which I generated cert.pem & key.pem and passed it to Net::Stomp::Client. I am using the Authen::Credential::x509 module for cert based authentication.
Now I am getting the following Error when I am trying to run the perl script:
DEBUG: .../IO/Socket/SSL.pm:605: socket not yet connected
DEBUG: .../IO/Socket/SSL.pm:607: socket connected
DEBUG: .../IO/Socket/SSL.pm:629: ssl handshake not started
DEBUG: .../IO/Socket/SSL.pm:662: using SNI with hostname mmx-nprd3-07.cisco.com
DEBUG: .../IO/Socket/SSL.pm:697: request OCSP stapling
DEBUG: .../IO/Socket/SSL.pm:787: ssl handshake done
# 2016/02/10-07:18:33 JmsClient.pl[6618]: connect to stomp+ssl://mmx-ntard-07.sammy.com:7222 ok: 160.**.1**.**2
Stomp Client Object Created
Connected to broker mmx-ntard-07.sammy.com (IP 1**.*8.**.**2), port 7222
# 2016/02/10-07:18:33 JmsClient.pl[6618]: Net::STOMP::Client=HASH(0x194df00)->connect()
# 2016/02/10-07:18:33 JmsClient.pl[6618]: encoding CONNECT frame
# 2016/02/10-07:18:33 JmsClient.pl[6618]: H accept-version:1.0,1.1,1.2
# 2016/02/10-07:18:33 JmsClient.pl[6618]: H host:mmx-ntard-07.sammy.com
# 2016/02/10-07:18:33 JmsClient.pl[6618]: sent 65 bytes
# 2016/02/10-07:18:33 JmsClient.pl[6618]: received 12 bytes
# 2016/02/10-07:18:34 JmsClient.pl[6618]: received 0 bytes
cannot sysread(): EOF
Below is the perl script :-
use Net::STOMP::Client;
use No::Worries::Log qw(*);
use IO::Socket::SSL;
use FindBin qw($Bin);
use Authen::Credential::x509;
$IO::Socket::SSL::DEBUG = 2;
log_filter("debug");
my $auth = Authen::Credential::x509->new(
# client certificate to present
cert => "$Bin/JmsCertificate/aix_jms_public_Cert.pem",
# # client private key
key => "$Bin/JmsCertificate/aix_jms_private_Key.pem",
pass => 'password'
);
my $stomp = Net::STOMP::Client->new(
uri => "stomp+ssl://mmx-ntard-07.sammy.com:7222",
auth => $auth,
debug => 'all',
);
print "\nStomp Client Object Created";
my $peer = $stomp->peer();
printf("\nConnected to broker %s (IP %s), port %d\n", $peer->host(), $peer->addr(), $peer->port());
$stomp->connect();
print "\nConnected\n";
my $sid = $stomp->uuid();
$stomp->subscribe(
destination => "/queue/test",
# we use the generated subscription id
id => $sid,
# we want a receipt on our SUBSCRIBE frame
receipt => $stomp->uuid(),
);
my $count = 0;
my $frame;
while ($count < 10) {
$frame = $stomp->wait_for_frames(timeout => 1);
if ($frame) {
if ($frame->command() eq "MESSAGE") {
$count++;
printf("received message %d with id %s\n",
$count, $frame->header("message-id"));
} else {
# this will catch the RECEIPT frame
printf("%s frame received\n", $frame->command());
}
} else {
print("waiting for messages...\n");
}
}
$stomp->unsubscribe(id => $sid);
$stomp->disconnect();
I am not able to figure out what is going on here. I saw couple of other posts too but they were for ActiveMQ. I'm not sure if that would make a difference.

Related

How to connect outlook IMAP server go through proxy server uisng perl

I want to connect IMAP server but i am not able to connect directly imap server that's why i used proxy but still i am not able to connect and read emails.
Following is my code,
#!/usr/intel/bin/perl
use strict;
use warnings;
# fill in your details here
my $username = 'username#companyname.com';
my $password = 'password';
my $mailhost = 'outlook.office365.com';#imap-mail.outlook.com
my $mailport = 993;
my $proxyhost = '121.244.253.5';
my $proxyport = 8080;
print "Proxy...\n";
use IO::Socket::Socks::Wrapper(
{
ProxyAddr => $proxyhost,
ProxyPort => $proxyport,
SocksDebug => 0,
Timeout => 100000000
}
);
# required modules
use Net::IMAP::Simple;
use Email::Simple;
use IO::Socket::SSL;
print "Connecting...\n";
$IO::Socket::SSL::DEBUG=2;
# Connect
my $imap = Net::IMAP::Simple->new(
$mailhost,
port => $mailport,
use_ssl => 1
) || die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr \n";
print "Logging In...\n";
# Log in
if ( !$imap->login( $username, $password ) ) {
print STDERR "Login failed: " . $imap->errstr . "\n";
exit(64);
}
print "Selecting Folder...\n";
# Look in the the INBOX
my $nm = $imap->select('Archive');
print "How Many Messages Are There...\n";
# How many messages are there?
my ($unseen, $recent, $num_messages) = $imap->status();
print "unseen: $unseen, recent: $recent, total: $num_messages\n\n";
print "Quickly Look for unseen messages...\n";
## Iterate through unseen messages
for ( my $i = 1 ; $i <= $nm ; $i++ ) {
if ( $imap->seen($i) ) {
next;
} else {
my $es = Email::Simple->new( join '', #{ $imap->top($i) } );
printf( "[%03d] %s\n\t%s\n", $i, $es->header('From'), $es->header(+'Subject') );
}
}
print "Disconnect...\n";
# Disconnect
$imap->quit;
print "Exit...\n";
exit;
Following is my Response :-
Proxy...
Connecting...
DEBUG: .../IO/Socket/SSL.pm:332: socket not yet connected
DEBUG: .../IO/Socket/SSL.pm:334: socket connected
DEBUG: .../IO/Socket/SSL.pm:347: ssl handshake not started
DEBUG: .../IO/Socket/SSL.pm:377: set socket to non-blocking to enforce timeout=100000000
I don't know why its not going further. Please share your opinion and correct me.
Please help me here...
Correct syntax is :
my $proxyhost = '65.130.4.202';
my $proxyport = 24451;
use Net::IMAP::Simple;
print "Proxy...\n";
use IO::Socket::Socks::Wrapper(
Net::IMAP::Simple => {
ProxyAddr => $proxyhost,
ProxyPort => $proxyport,
SocksDebug => 3,
Timeout => 100000000
}
);
use IO::Socket::SSL;
print "Connecting...\n";
$IO::Socket::SSL::DEBUG=2;
# Connect
my $imap = Net::IMAP::Simple->new(
'imap.gmail.com',
port => 993,
use_ssl => 1
) || die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr \n";
print "Logging In...\n";
# Log in
if ( !$imap->login( 'pappucant#gmail.com', 'pappu123' ) ) {
print STDERR "Login failed: " . $imap->errstr . "\n";
exit(64);
}
Working fine for me, didn't work with the proxy server mentioned in the code.
Output:
Proxy...
Connecting...
DEBUG: .../IO/Socket/SSL.pm:625: socket not yet connected
DEBUG: .../IO/Socket/SSL.pm:627: socket connected
DEBUG: .../IO/Socket/SSL.pm:649: ssl handshake not started
DEBUG: .../IO/Socket/SSL.pm:682: using SNI with hostname imap.gmail.com
DEBUG: .../IO/Socket/SSL.pm:738: set socket to non-blocking to enforce timeout=90
DEBUG: .../IO/Socket/SSL.pm:764: ssl handshake in progress
DEBUG: .../IO/Socket/SSL.pm:774: waiting for fd to become ready: SSL wants a read first
DEBUG: .../IO/Socket/SSL.pm:794: socket ready, retrying connect
DEBUG: .../IO/Socket/SSL.pm:764: ssl handshake in progress
DEBUG: .../IO/Socket/SSL.pm:774: waiting for fd to become ready: SSL wants a read first
DEBUG: .../IO/Socket/SSL.pm:794: socket ready, retrying connect
DEBUG: .../IO/Socket/SSL.pm:764: ssl handshake in progress
DEBUG: .../IO/Socket/SSL.pm:774: waiting for fd to become ready: SSL wants a read first
DEBUG: .../IO/Socket/SSL.pm:794: socket ready, retrying connect
DEBUG: .../IO/Socket/SSL.pm:809: ssl handshake done
Logging In...
Login failed: [AUTHENTICATIONFAILED] Invalid credentials (Failure)

Unable to connect to outlook.office365.com for processing emails with Perl Module Mail::IMAPClient

I have searched for days but finally run out of options, one of my clients move its emails accounts to "outlook.office365.com", now all my perl scripts fails because they have a different set of configurations on their platform, so i had to rewrite all of my perl scripts to make everything work again, i was aible to send emails with the module "Email::Sender::Transport::SMTP::TLS" but i didnt had the same luck using my old modules to read and proccess emails such as "Mail::POP3Client".
This is the code a try:
use Mail::IMAPClient;
use Email::MIME;
my $username = 'user';
my $password = 'pass';
my $server = 'outlook.office365.com';
print "Anon connect to IMAP\n";
my $imap = Mail::IMAPClient->new(
Server => $server,
Username => $username,
Password => $password,
Port => 993,
Ssl => 1,
Authmechanism => "PLAIN",
Debug => 1,
)or die "Cannot connect to $mailhost as $username: $#";
print "upgrading connection to TLS \n";
$imap->starttls
(
SSL_verify_mode => 0,
) or die "starttls failed: $#\n";
$imap->User($username);
$imap->Password($password);
print "Logging In\n";
$imap->login() or die "imap login failed: $#\n";
This is a solution that i have found on stackoverflow but it doesnt work for me, i checked my firewall configuration and everything is ok.
This is the output:
Anon connect to IMAP
Started at Fri Dec 16 13:54:25 2016
Using Mail::IMAPClient version 3.38 on perl 5.022001
Connecting with IO::Socket::SSL PeerAddr outlook.office365.com PeerPort 993 Proto tcp Timeout 600 Debug 1
ERROR: Unable to connect to outlook.office365.com: at C:/Perl64/site/lib/Mail/IMAPClient.pm line 370.
Mail::IMAPClient::connect(Mail::IMAPClient=HASH(0xcac170)) called at C:/Perl64/site/lib/Mail/IMAPClient.pm line 314
Mail::IMAPClient::new("Mail::IMAPClient", "Server", "outlook.office365.com", "Username", "user", "Password", "pass", "Port", 993, ...) called at dont.pl line 10
Cannot connect to as user: Unable to connect to outlook.office365.com: at dont.pl line 10.
To check my SSL connection:
openssl s_client -connect outlook.office365.com:993
Results:
* OK The Microsoft Exchange IMAP4 service is ready.
So any ideas?, what is wrong with this code?
EDIT: thanks to Max, finally a solution: removing startls allows to reach the login step.
use Mail::IMAPClient;
use Email::MIME;
my $username = 'user';
my $password = 'pass';
my $server = 'outlook.office365.com';
print "Anon connect to IMAP\n";
my $imap = Mail::IMAPClient->new(
Server => $server,
Port => 993,
Ssl => 1,
Authmechanism => "PLAIN",
Debug => 1,
)or die "Cannot connect to $mailhost as $username: $#";
$imap->User($username);
$imap->Password($password);
print "Logging In\n";
$imap->login() or die "imap login failed: $#\n";
On my machine, it outputs this:
Anon connect to IMAP
Started at Fri Dec 16 16:59:31 2016
Using Mail::IMAPClient version 3.38 on perl 5.022001
Read: * OK The Microsoft Exchange IMAP4 service is ready. [RABCADYAUABSADAAMgAwADEAQwBBADAAMAAyADcALgBlAHUAcgBwAHIAZAAwADIALgBwAHIAbwBkAC4AbwB1AHQAbABvAG8AawAuAGMAbwBtAA==]
upgrading connection to TLS
Sending: 1 CAPABILITY
Sent 14 bytes
Read: * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+
1 OK CAPABILITY completed.
Logging In
Sending: 2 AUTHENTICATE PLAIN
Sent 22 bytes
Read: +
Sending: [Redact: Count=2 Showcredentials=OFF]
Sent 18 bytes
Read: 2 NO AUTHENTICATE failed.
ERROR: 2 NO AUTHENTICATE failed. at /usr/local/share/perl/5.22.1/Mail/IMAPClient.pm line 3261.
Mail::IMAPClient::authenticate(Mail::IMAPClient=HASH(0x14f6a30), "PLAIN", undef) called at /usr/local/share/perl/5.22.1/Mail/IMAPClient.pm line 562
Mail::IMAPClient::login(Mail::IMAPClient=HASH(0x14f6a30)) called at imapt.pl line 30
imap login failed: 2 NO AUTHENTICATE failed.
Which is normal, as I don't have a valid account.

Error Connecting to a JMS Topic Broker via SSL using Net::STOMP::Client module in Perl

I am trying to connect to a TIBCO JMS Topic Broker with the help of Net::STOMP::Client package in perl.
I am using the SSL approach while creating a new Net::STOMP::Client Object and passing the attributes 'uri' & 'sockopts' where it takes the Topic Uri & SSL Certificate files for Authentication.
When I try to run this script it throws an error saying : -
cannot SSL connect to mmx-nprd1-06:7222: IO::Socket::INET6 configuration failed
Code is give below :-
use Net::Stomp;
use Net::STOMP::Client;
use Moose;
use strict;
use warnings;
use FindBin qw($Bin);
print "\n$Bin\n";
my $stomp;
$stomp = Net::STOMP::Client->new(
uri => "stomp+ssl://mmx-nprd1-06:7222",
sockopts => {
# path of the directory containing trusted certificates
SSL_ca_path => "$Bin/JmsCertificate/",
# client certificate to present
SSL_cert_file => "$Bin/JmsCertificate/aix_jms_cert.pem",
# # client private key
SSL_key_file => "$Bin/JmsCertificate/aix_jms_key.pem",
# passphrase of the client private key
SSL_passwd_cb => sub { return("password") },
},
);
$stomp->connect();
my $peer = $stomp->peer();
printf("connected to broker %s (IP %s), port %d\n", $peer->host(), $peer->addr(), $peer->port());
my $sid = $stomp->uuid();
$stomp->subscribe(
destination => "/queue/test",
# we use the generated subscription id
id => $sid,
# we want a receipt on our SUBSCRIBE frame
receipt => $stomp->uuid(),
);
my $count = 0;
my $frame;
while ($count < 10) {
$frame = $stomp->wait_for_frames(timeout => 1);
if ($frame) {
if ($frame->command() eq "MESSAGE") {
$count++;
printf("received message %d with id %s\n",
$count, $frame->header("message-id"));
} else {
# this will catch the RECEIPT frame
printf("%s frame received\n", $frame->command());
}
} else {
print("waiting for messages...\n");
}
}
$stomp->unsubscribe(id => $sid);
$stomp->disconnect();
Can Someone help me out with this as I am not able to figure out whats going wrong here.
I also had some issues using the uri key try the following :
$stomp = Net::STOMP::Client->new(host => "mmx-nprd1-06", port => 7222,
sockopts => {
# path of the directory containing trusted certificates
SSL_ca_path => "$Bin/JmsCertificate/",
# client certificate to present
SSL_cert_file => "$Bin/JmsCertificate/aix_jms_cert.pem",
# # client private key
SSL_key_file => "$Bin/JmsCertificate/aix_jms_key.pem",
# passphrase of the client private key
SSL_passwd_cb => sub { return("password") }, # Leave this out if your key doesn't require a passphrase
},
);
$stomp->connect(login => "aix_jms", passcode => "some_password");

connect to localhost failed (Connection refused) no (more) retries

I want to send an email using perl ,but when i execute the command as follows:
#./sendmail.sh "par1" "par2" "par3"
i got the error msg "connect to localhost failed (Connection refused) no (more) retries"
sendmail.sh:
/usr/bin/perl /code/sendmail.pl "$1" "$2" "$3";
sendmail.pl:
#!/usr/bin/perl -w
use Mail::Sendmail;
my $event1 = shift(#ARGV);
my $event2 = shift(#ARGV);
my $time = shift(#ARGV);
#my $info = shift(#ARGV);
my $datetime = `/bin/date "+20%y-%m-%d %H:%M:%S"`;
chomp $datetime;
$msg = "This is Monitor System speak:\n
The system discovers the events at $datetime.
Something may be abnormal, please check it. The detail is below:\n";
$msg = $msg."$event1 and $event2 at $time\n";
$msg = $msg."\n";
$msg = $msg."Any problem, check it from http://map_test.php\n\n\n";
$mail_subject = "Abnormal";
sendmail(
From => 'localhost',
To => 'test#mail.com',
Subject => $mail_subject,
Message => $msg,
);
Any help appreciated.
smtp stands for simple mail transfer protocol.
When you need to send an email your mail client needs to talk to an smtp server which will accept the message. Normally your internet service provider will provide an smtp host. If you look at your mail client it will need to have an smtp server configured to be able to send mail.
Ok so when you install the Mail::Sendmail module, it doesn't know what your smtp server will be. It is up to you to tell it. It provides a default of localhost which would often be true if your server is running a sendmail daemon.
The configuration of Mail::Sendmail is stored in a variable called
%Mail::Sendmail::mailcfg
You can change the value of the sendmail server using this snippet of code:
unshift #{$Mail::Sendmail::mailcfg{'smtp'}} , 'my.smtp.server';
You need to add this line of code to your script to set the smtp server.
It adds this server to an array which also includes localhost.
So if neither of the hosts work it will still print an error message about localhost which is slightly confusing.
If you use Data::Dumper to print the contents of the mailcfg variable it will look something like this:
#!/usr/bin/perl
use Mail::Sendmail;
use Data::Dumper;
unshift #{$Mail::Sendmail::mailcfg{'smtp'}} , 'my.smtp.server';
print Dumper(\%Mail::Sendmail::mailcfg);
Should return:
$VAR1 = {
'retries' => 1,
'smtp' => [
'my.smtp.server',
'localhost'
],
'delay' => 1,
'port' => 25,
'from' => '',
'debug' => 0,
'tz' => '',
'mime' => 1
};

Can't call method "headers" on an undefined value at C:/Perl/site/lib/Net/Stomp. pm line 122

I am trying to send EMS message from Perl to queue running in the EMS server. I'm using STOMP module to connect to EMS queue for sending message. Here is my code -
JMSQUEUE.pl:
use Net::Stomp;
use Net::Stomp::Frame;
my $stomp = Net::Stomp->new( { hostname => 'localhost', port => '7222' } );
$stomp->connect( { login => 'admin', passcode => '' } );
$stomp->send( { destination => '/queue/pradeepexp', body => 'test message' } );
$stomp->disconnect;
and in my module - STOMP.PM:
sub connect {
my ( $self, $conf ) = #_;
my $frame =
Net::Stomp::Frame->new( { command => 'CONNECT', headers => $conf } );
$self->send_frame($frame);
$frame = $self->receive_frame;
# Setting initial values for session id, as given from
# the stomp server
$self->session_id( $frame->headers->{session} );
$self->_connect_headers($conf);
return $frame;
}
Any settings I need to do before calling connect?
I had the same problem with sending messages from Perl to ApacheMQ.
(Perl + Net-Stomp-0.45 + apache-activemq-5.8)
It was just a little mistake.
It is important to set the correct transportConnectors in this file harddisk\apache-activemq-5.8\conf\activemq.xml.
<transportConnectors>
<transportConnector name="stomp" uri="stomp://localhost:61616"/>
</transportConnectors>
After that it works fine :D
For more information: http://activemq.apache.org/stomp.html
Maybe there are similar files in EMS.
This happens because the Stomp library receives an invalid (or no) response from the message broker.
Try to telnet to the message broker and see if it speaks Stomp at all.