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

Hey there Monks, i'd like to extract the logs of a mikrotik router through telnet. The problem with my code is that it won't wait for the prompts to appear before executing the next command. Can you help me with it?
sub get_log { my $client = new Net::Telnet; open(LOG,">/tmp/telnet.log"); $client->dump_log("/tmp/dump.log"); $client->open($_[0]); my $prompt = '/user\@Becse/i'; $client->prompt($prompt); $client->login('user','pass'); $client->cmd('/log'); my $cmd = 'print without-paging'; my @lines = $client->cmd($cmd); $client->cmd('/'); $client->cmd('quit'); $client->close; foreach (@lines) { print LOG "$_\n"; } close(LOG); return @lines; }

Replies are listed 'Best First'.
Re: Telnet extract output
by tilly (Archbishop) on Jul 31, 2008 at 23:02 UTC
    I believe you have misdiagnosed your problem. Your most likely problem is that you are issuing a directory name as a command expecting to move to that directory, and you're not seeing that the telnet session is complaining that that is a directory, not a file.

    Try modifying your commands to things like $code->cmd('cd /log'); and see if your problems go away. If they don't then I highly recommend trying to echo the results of the telnet session to screen while you're trying to debug it.

    I would also suggest that you try to add in some error handling logic if you're getting responses other than what you're expecting. Be warned that error handling logic is notoriously hard to add when manipulating programs remotely like this, and that is likely to be a source of future problems. But at least by trying to add some checks now you can ease many of your future headaches.

      In the MikrotikOS telnet session you can change directory just typing the path, without any commands. I can see the session in the dump.log file and my problem is exactly what i said before. :(
        Does your prompt match the actual prompt?

        Could your prompt possibly match some data somewhere in the logs? That would cause it to think output has stopped when it hasn't.

        Make your prompt the most restrictive thing that you can. If you can, it may help to issue a command to change your prompt to something you're sure is not in the logs (setting the PS1 environment variable does it in bash) and setting your prompt to the new prompt.

Re: Telnet extract output
by marcussen (Pilgrim) on Aug 01, 2008 at 05:17 UTC

    Perhaps you wanted Net::Cmd instead of Net::Telnet

    Confucius says kill mosquito unless cannon
Re: Telnet extract output
by Anonymous Monk on Aug 01, 2008 at 08:43 UTC
    I am facing the same problem where first commands output goes to second command .Ideally second should not even start runnning before first gets completed to give the prompt back.