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

use strict; use warnings; use Expect; my $command = Expect->spawn("telnet 000.000.000.000"); print "looking for 'login:'\n"; unless($command->expect(10, "login:")) { die "timed out"; } print $command "<user>\n"; unless($command->expect(60, -re=> 'assword: ?')) { die "timed out"; } print $command "<password>\n"; unless($command->expect(60, -re=> 'cr.*')) { die "timed out"; } print "------------------\n"; print $command "show inventory\n"; $command->hard_close();
I am using the above to run the command 'show inventory' on a router I am logging into. The command does not run, should it work? Am I going about this wrong?

Replies are listed 'Best First'.
Re: use telnet to router with expect
by RMGir (Prior) on Mar 14, 2014 at 13:18 UTC
    "Does not run" meaning the spawn fails? Or at a later step? You might want to add an
    or die "Expect spawn failed, error $!";
    to the end of your spawn command - that might tell you WHY it's failing...

    I assume 000.000.000.000 isn't the IP address you're actually using - but if it is, that would explain the failure :)


    Mike
      thanks Mike...everything works except the 'show inventory' command. The login succeeds and I see the router prompt. At that point, the script says: 'print $command "show inventory"' and the command does not execute.
        Interesting...

        I'd download a copy of wireshark (a.k.a. tcpdump) and look at what goes over the wire when you do the command interactively with telnet - perhaps there's a \r sent somewhere you're using a \n, for example.


        Mike
        Another possibility - maybe "hard_close" is closing the command before the server can respond?

        What happens if you switch that to "soft_close", or even expect something you'd see at the end of the inventory before closing?


        Mike