s2cuts has asked for the wisdom of the Perl Monks concerning the following question:

Hello all! I'm scripting to a daemon through Net::Telnet, and I've found that the daemon does not produce an easily identifiable prompt. It's just an empty line with a blinking cursor on it. Is anyone aware of a technique to attack this problem with Net::Telnet? I'm really hoping to avoid Expect... Thanks all.
  • Comment on Net::Telnet: how to detect an empty line...

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

Re: Net::Telnet: how to detect an empty line...
by apl (Monsignor) on Feb 27, 2008 at 03:12 UTC
    You might want to provide a little more detail on what you're trying to do. Daemons don't prompt for input.
      Maybe daemon isn't the best way to describe it. I'll call it a process. This process has built into it the capability to allow users to interact with it through telnet. You would most likely set parameter, look up parameters, stuff like that. I happen to be scripting to it, and have discovered that the user's prompt is only an empty line with a blinking cursor on it.

      So my question is, how do I approach this problem using Net::Telnet. Net::Telnet is very simple to use, and I would prefer using it. I suppose I could time the commands, but that doesn't quite sit well with me. Any advice would be appreciated.
Re: Net::Telnet: how to detect an empty line...
by glide (Pilgrim) on Feb 27, 2008 at 12:41 UTC
    Hi,
    probably you can use a empty line has a prompt in the Net::Telnet
    $t = new Net::Telnet (Prompt => ’/^$/’);

    But, if you have empty lines in the command output, you'll get a truncated output. In that case you can use the waitfor method.

    And to better understand the interaction between the client e server, use the input_log and the dump_log, it's a great tool for debug.