in reply to Net::Telnet: how to detect an empty line...

Perhaps Net::Telnet is not the right module to use. You might try IO::Socket::INET. If it's a server like an HTTP server you probably don't need any of the telnet option negotiation stuff anyway.

Does it respond when you send it commands? Then you can just assume that you have a 'prompt' if you get connected to it. Send a command and wait for a response. Then send the next command, etc. Here's how it could work using IO::Socket::INET:

use IO::Socket::INET; my $s = new IO::Socket::INET(PeerAddr => ..., PeerPort => ..., Proto = +> 'tcp'); unless ($s) { die "unable to connect..." } # we are connected, so just start sending commands print $s "first command\r\n"; my $resp = <$s>; # expect one line response print $s "second command\r\n"; ...

Replies are listed 'Best First'.
Re^2: Net::Telnet: how to detect an empty line...
by s2cuts (Acolyte) on Feb 27, 2008 at 04:41 UTC
    Ok, that looks like a solution. Question: if I'm expecting a muli-line response, can I just simply make $resp (from your example above) to @resp, and will the following print command wait for an entire 'response'?
      Well, you need to know when to stop reading lines otherwise you'll block (i.e. your program will just stop and wait for the other end to send data.) So, you either need to know exactly how many lines are going to be returned or look for a special pattern in the output.

      If you really can't determine when the server has completed its response, you can guess based on the heuristic that if the server hasn't sent any data for X number of seconds and we're at the beginning of a line then just assume the server is done sending data.

        Hey thanks for your help pc88mxer. I've read through the relatively short documentation for IO::Socket::INET, and I can't get the code below to work. Unfortunately, I don't think I know enough about network programming and or perl to get much farther on my own. It's passing the unless test, and exiting. The process is running, and I can connect to it with telnet.
        my @resp; my @all_resp; my $s = new IO::Socket::INET( PeerAddr => 'localhost', Peerport => 400 +0, Proto => 'tcp', Timeout => 4); print "before unless"; unless ($s) { die "unable to connect..." } sleep 1; # we are connected, so just start sending commands print $s "auth admin 555555\r\n"; sleep 1; @resp = <$s>; # expect one line response push (@all_resp, @resp); print $s "voo allowed_ips\r\n"; sleep 1; @resp = <$s>; push (@all_resp, @resp); print @all_resp;