Perl HTTP Tiny + Socket Socks Wrapper Timeout not working - perl

I am using HTTP::Tiny + IO::Socket::Socks::Wrapper to send a HTTP Request over a SOCKS Proxy. Everything is working fine except the timeout option.
When using only IO::Socket::Socks without HTTP::Tiny the timeout is working.
Example without HTTP::Tiny and a non existent proxy to trigger a timeout:
my $t = time;
my $sock = IO::Socket::Socks->new(
ProxyAddr => '4.5.6.7',
ProxyPort => 1080,
ConnectAddr => 'www.google.com',
ConnectPort => 80,
Timeout => 3
) or print "connection failed or timed out\n";
print "time: " . (time - $t) . "\n";
Output:
connection failed or timed out
time: 3.00517201423645
Example with HTTP::Tiny:
my $t = time;
my $http = wrap_connection(
HTTP::Tiny->new(timeout => 3), {
ProxyAddr => '4.5.6.7',
ProxyPort => 1080,
Timeout => 3
}
);
my $r = $http->get("http://www.google.com");
print "connection failed or timed out\n" unless $r->{success};
print "time: " . (time - $t) . "\n";
Output:
connection failed or timed out
time: 21.0282030105591
Why does the second example not timeout after 3 seconds?

It was bug, which now seems to be fixed. New version will be uploaded to CPAN soon.
And right now you can get fixed version from github repo.

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)

Reuse a Perl IO::socket::ssl client

I'm trying to lighten a part of my code where I was user an LWP::UA
To acces a Rest API using a SSL Layer.
I'm trying to achieve the same result with the bare IO::Socket::SSL module.
It work great on the first call with something like this :
my $SSLSocket = IO::Socket::SSL->new(
PeerHost => $server,
PeerPort => "https");
print $SSLSocket "GET /restapi/json/v1/resources?AUTHTOKEN=$token HTTP/1.0\r\n\r\n";
while(<$SSLSocket>){
$response = $_;
}
$resources = $json->decode($response);
But when I'm trying to reuse this socket I'm getting no awnser
my $SSLSocket = IO::Socket::SSL->new(
PeerHost => $server,
PeerPort => "https");
print $SSLSocket "GET /restapi/json/v1/resources?AUTHTOKEN=$token HTTP/1.0\r\n\r\n";
while(<$SSLSocket>){
$response = $_;
}
$resources = $json->decode($response);
print $SSLSocket ,"GET /restapi/json/v1/resources/3138/accounts?AUTHTOKEN=$token HTTP/1.0\r\n\r\n";
while(<$SSLSocket >){
$response = $_;
}
close($SSLSocket );
my $accounts = $json->decode($response);
The second read is empty.
Since I cannot use the recv or send method I'm a bit lost.
Is there any way to not open a new SSL Socket on each request ?
Thank you for you answers.

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
};

How to fail over

If I use wget to retrieve something from the geonames.org server, it reports two IP addresses, and the first one fails but it gets it from the second:
Resolving ws.geonames.org (ws.geonames.org)... 5.9.41.208, 176.9.107.169
Connecting to ws.geonames.org (ws.geonames.org)|5.9.41.208|:80... failed: Connection refused.
Connecting to ws.geonames.org (ws.geonames.org)|176.9.107.169|:80... connected.
HTTP request sent, awaiting response... 200 OK
But unfortunately I have to access it through perl using LWP::UserAgent and HTTP::Request. How can I make them try the second IP if the first fails?
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(
GET =>
"http://ws.geonames.org/countrySubdivision?lat=$lat&lng=$long&radius=$radius&username=xyzzy");
my $res = $ua->request($req);
You can do it yourself: get all the IP addresses with the help of Net::DNS::Resolver, and then try all IP addresses until you get a successful response. Note that you have to supply the "Host" header yourself if working with an IP address, in case the server is doing name-based virtual hosts.
Something like the following lines could work. Maybe there's even a CPAN module for this, I did not check:
use Net::DNS;
use LWP::UserAgent;
my #addrs;
{
my $res = Net::DNS::Resolver->new;
my $query = $res->search("ws.geonames.org");
if ($query) {
for my $rr ($query->answer) {
if ($rr->type eq "A") {
push #addrs, $rr->address;
}
}
} else {
die "DNS query failed: ", $res->errorstring, "\n";
}
}
my $ua = LWP::UserAgent->new;
my $res;
for my $addr (#addrs) {
$res = $ua->get("http://$addr/countrySubdivision?lat=$lat&lng=$long&radius=$radius&username=xyzzy", Host => 'ws.geonames.org');
last if $res->is_success;
}
The solution from Slaven is OK except when the IP addresses are not directly accessible.
In that case, the following works for me:
local #LWP::Protocol::http::EXTRA_SOCK_OPTS = (
PeerAddr => 'my_hostname',
MultiHomed => 1,
);
my $response = $ua->post('https://my_hostname/...', ...);

Perl SSH connection to execute telnet

I tried the following to access a router via a central admin server as "ssh hop" server
#!/usr/bin/perl -X
use strict;
use Net::OpenSSH;
use Net::Telnet;
my $lhost = "linuxserver";
my $luser = "linuxuser";
my $lpass = "linuxpassword";
my $chost = "routername";
my $cpass = "Routerpassword";
my $prompt = '/(?:Password: |[>])/m';
my #commands = ("show users\r");
my $ssh = Net::OpenSSH->new($lhost,
'user' => $luser,
'password' => $lpass,
'master_opts' => [ '-t' ],
#'async' => 1 # if enabled then password cannot be set here
);
my ($pty, $err, $pid) = $ssh->open2pty("telnet $chost");
my $t = new Net::Telnet(
-telnetmode => 0,
-fhopen => $pty,
-prompt => $prompt,
-cmd_remove_mode => 1,
-output_record_separator => "\r",
#-dump_log => "debug.log",
);
my $end = 0;
while (!$end) {
my ($pre, $post) = $t->waitfor($prompt);
if ($post =~ /Password: /m) {
# send password
$t->print("$cpass");
}
elsif ($post =~ /[>#]/ && #commands) {
my $cmd = shift(#commands);
if ($cmd !~ /[\r\n]/) {
$t->print($cmd);
}
else {
print $t->cmd($cmd);
}
}
else {
$end = 1;
$t->cmd("exit");
}
}
#close $pty;
$t->close();
Unfortunately I always get the following error:
read error: Input/output error at test.pl line 71
Can somebody help me please or is there a better solution only to test if a telnet connection via the "hop" server is possible or not?
The connection looks like:
workstation --ssh-> server --telnet-> router
Thanks in advance.
I think best option is to make an SSH-tunnel to your admin server and use it for telnetting to the router.
Getting Net::Telnet to work over Net::OpenSSH sometimes is not as easy as it should be and it requires some experimentation to get to the right combination of flags and calls that make it work.
For instance, instead of telneting to the target host, use netcat to open a raw connection (or Net::OpenSSH support for TCP forwarding if tunnels are allowed on the proxy).
Expect + Net::OpenSSH may be a better option.