My Perl skills are pretty rudimentary.

I am having a bit of trouble converting a Perl telnet script to use SSL. 
The original script uses Net::Telnet to do its work, but I can't seem to 
find any drop-in replacement that uses SSL but preserves the 
functionality of Net::Telnet (particularly the getline function). It is 
for a Nagios plugin, so using something like stunnel is a little to 
heavy weight (though I may end up doing that if I can't get it to work 
properly).

The problem is that I can't seem to clearly tell when the server is done 
transmitting after I send each (or the first :-) command, so the far 
side ends up timing out and closing the connection waiting for me to 
send something.

Below is a simple Perl script which illustrates the problem I am having. 
It is based on a socket example I saw in my surfing. In theory, since 
pipelining is requested in the headers of the first GET, the socket 
should remain open and the second GET should work. What is happening is 
the @response = <$client> line is waiting until the socket closes before 
continuing and the second GET obviously fails. Keep in mind, this isn't 
about get web pages, the LWP modules solve that problem. I am just using 
this to illustrate the problem I am having talking with an interactive 
SSL daemon.

I have tried a few variations (like while (<$client>) etc) trying to 
detect when there is no more data being sent, but it occurs to me that 
may require some token in the data stream itself, which may be tricky. 
The fact is I know very little about socket programming having been lazy 
all my life by using CPAN for everything, and this is likely a simple 
thing that I am not able to grok from my googling.

Any ideas? And thanks in advance for any help.

Adam Augustine

#!/usr/bin/perl -w

use strict;
use IO::Socket::SSL;

my $site="www.example.com";
my $client = IO::Socket::SSL->new("$site:https");
my @response=();
my $line="";
my $send_msg="GET / HTTP/1.1\r
Host: " . $site . "\r
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) 
Gecko/20071025 Firefox/2.0.0.9\r
Accept: 
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r
Accept-Language: en-us,en;q=0.5\r
Accept-Encoding: gzip,deflate\r
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r
Keep-Alive: 300\r
Connection: keep-alive\r
Pragma: no-cache\r
Cache-Control: no-cache\r\n";

if ($client) {
    print "sending:\n";
    print $send_msg;
    print $client $send_msg, "\r\n";
    print "1\n";
    chomp (@response=<$client>);
    foreach $line (@response) {
        print "foreach line: $line\n";
    }
    print "2\n";
    print $client $send_msg, "\r\n";
    print "3\n";
    chomp (@response = <$client>);
    foreach $line (@response) {
        print "2nd print line: $line\n";
    }
    print "4\n";
    close $client;
} else {
    warn "I encountered a problem: ", IO::Socket::SSL::errstr();
}
--------------------
BYU Unix Users Group 
http://uug.byu.edu/ 

The opinions expressed in this message are the responsibility of their
author.  They are not endorsed by BYU, the BYU CS Department or BYU-UUG. 
___________________________________________________________________
List Info: http://uug.byu.edu/mailman/listinfo/uug-list

Reply via email to