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

Hi,

I'm trying to do something quite simple with Net:Telnet. I'm trying to log into a router, do a 'sh run' (or some other command) and print it to a file. Dead easy, right? I wish. Ok, here's what the code looks like:

#!/usr/bin/perl -w use Net::Telnet; use strict; my $router = "212.31.213.106"; my $t=new Net::Telnet(); my $instr = "sh ver"; $t->open($router); #...does login stuff here...# $t->cmd("term len 0"); $t->cmd("sh run"); my @config = $t->cmd($instr); $t->cmd(" "); my $file = "/home/me/logs/blah"; open(FILE, ">$file"); print FILE "@config";

Now, the weirdness begins. See how I've got 'sh run' as a straight command, then '@config = $t->cmd("$instr")'? Well when I print @config to the file down the bottom, it prints the output of the 'sh run'. And if I change the command before from 'sh run' to something else, it does that. NEVER 'sh ver'. If I remove the command in the 'sh run' line, the @config array remains undefined.

HELP!

Replies are listed 'Best First'.
Re: Net::Telnet variable assignment wacky
by sgifford (Prior) on Sep 29, 2003 at 16:04 UTC
    Perhaps it's not able to detect the prompt on your router correctly. What is the prompt on your router? What happens if you capture the output to sh run into a list?
      The prompt problem is rife with net::telnet, yeah, but that's not the problem as far as I can see. In the instance where the 'sh run' line is commented out, and all I have running is '@config = $t->cmd("sh ver")' then my input log shows that the command is working. But when I try to print out @config, I get nothing. The only time @config gets assigned a value of any sort is when I precede the array assignment with a bare command like the 'sh run' line.
        Few suggestions to try
        - Define your break character (output record separator) (by default it is "\n")
        - Define your router prompt
        - Use dump_log() command to see what's happenning in the session

        bonoboy - Just to clarify, everything works ok when you do this:

        @config = $t->cmd("sh ver");
        But doesn't appear to work doing this:
        my $command = "sh ver"; @config = $t->cmd($command);

        Just want to make sure I understand what the problem is.

        -- vek --