Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Telnet handshaking

by rq-2102 (Acolyte)
on May 17, 2001 at 22:01 UTC ( [id://81316]=perlquestion: print w/replies, xml ) Need Help??

rq-2102 has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a program that scans through a list of servers that i have and connects to them all one by one to make sure that they are valid servers, however if i happen to pass over a telnet server, my program hangs up during the handshaking process. Normally, i would go ahead and have the program complete the whole handshaking process but since it's uneccessary for this particular program i'd rather not do that if possible, i've tried different ways of making the connection timeout all to no avail, here's some of the code i'm using to make the connection. Any ideas would be very much appreciated.

shortened for brevity:
-------------------------------------------------

&connect_host; send(SOCKET,$begin, 0); while (defined($_ = <SOCKET>)) { if ($_ =~ /3point\>/ig){ $valid = 1; close SOCKET; } else { $valid = 0; close SOCKET; } } if ($valid == 1){ print "valid server\n"; } else { print "invalid server\n"; } sub connect_host{ $iaddr = inet_aton($host) or die "ERROR1: $!\n"; $port = sockaddr_in($port, $iaddr) or die "ERROR2: $!\n"; $proto = getprotobyname('tcp') or die "ERROR3: $!\n"; socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "ERROR4: $!\n"; connect(SOCKET, $port) or die "ERROR5: $!\n"; }
---------

:|

Edit: chipmunk 2001-05-17

Replies are listed 'Best First'.
Re: Telnet handshaking
by ncw (Friar) on May 18, 2001 at 02:10 UTC
    The telnet port probably isn't opening properly because you aren't completing the telnet negotiation sequence. I don't know whether you are familiar with this but it involves sending small cryptic binary messages to the server to negotiate with the client what options they both support. I wrote a C program to do this a while back - it just NAKed all the options the server sent it.

    I discovered that some servers won't start up at all unless you perform the negotiation (Linux telnetd is one) whereas others are quite happy for you to charge in there ignoring the negotiation.

    You could write the code for this, but as you've noted it is a bit of a pain. However you've got several other options.

    • Investigate Net::Telnet to negotiate for you
    • Investigate IO::Socket to make your connections and in particular the timeout parameter so you know when the connections have failed
    • Learn how to make nonblocking sockets The Perl Cookbook will help here.
    • Set an alarm - you could use my time limit module for instance.
    TIMTOWDI of course!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://81316]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found