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

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'?
  • Comment on Re^2: Net::Telnet: how to detect an empty line...

Replies are listed 'Best First'.
Re^3: Net::Telnet: how to detect an empty line...
by pc88mxer (Vicar) on Feb 27, 2008 at 05:40 UTC
    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;

        Like pc88mxer already said, you need to know when to stop to read. Your code

        @resp = <$s>; # expect one line response

        will read from $s until your daemon stops or closes the socket, contrary to your comment that it will only read one line. Try it with the following code:

        @resp = (); while (defined $_ = <$s> and ! /^\s*$/) { warn "DEBUG: Got >$_<"; push @resp, $_; };

        The above loop will output some debug information and read until the next empty line. If something goes wrong, you will easily see that from the debug information ...