Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Net::Telnet :: How to check for the success of 'cmd'

by thens (Scribe)
on Oct 09, 2003 at 16:44 UTC ( #297987=perlquestion: print w/replies, xml ) Need Help??

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

Iam using Net::Telnet to issue some commands to a remote server. I have no problems in connecting to the server and executing the commands. I always get a return status of '1' from the cmd() even if the command fails. How should I check for the return status of the cmd() in Net::Telnet module.

use strict; use warnings; my $t = new Net::Telnet ( Prompt => '/.*\d+\s*%>.*$/', Timeout => 15, ); my ( $hostname, $username, $passwd ) = ( 'xxx', 'xxx', 'xxx' ); $t->open($hostname); $t->login($username, $passwd) or die "Cannot connect to the server"; print "Connected to $hostname as '$username'\n"; # This does not seem to work #$t->errmode(sub { warn @_ } ); my ( @output, $output ); # Change to some directory that is not there # It gives the output properly with error messages if any @output = $t->cmd ("/bin/cd not_there"); print "@output"; # always returns 1 $output = $t->cmd ("/bin/cd not_there"); print $output;
Update : Can I use echo $? after every command to know the status of the commands. Will Net::Telnet do anything in between the actual command and the echo $? to change $?. Please advise.

Hope Iam clear


use perl; use strict;

Replies are listed 'Best First'.
Re: Net::Telnet :: How to check for the success of 'cmd'
by phydeauxarff (Priest) on Oct 09, 2003 at 23:55 UTC
    have you tried using getlines() after your $t->cmd to see more of what is being returned?

    You might also check out print() as an alternative to cmd() as outlined in the net::telnet documentation.

    As a last resort if you aren't getting the results you expect, I have always found dump_log() to be a good, good friend.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://297987]
Approved by HyperZonk
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2023-12-05 18:48 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (27 votes). Check out past polls.