Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Can't get cmd_remove_mode to work in Net::Telnet

by gctaylor1 (Hermit)
on Jan 15, 2009 at 20:53 UTC ( #736670=perlquestion: print w/ replies, xml ) Need Help??
gctaylor1 has asked for the wisdom of the Perl Monks concerning the following question:

When I use Net::Telnet my command works but it also echoes the command and I want just the results. The docs for Net::Telnet talk about this and suggest using cmd_remove_mode, yet I still get the same result in my output. I've been working on this silly problem for hours. It seems so simple but I cannot figure out what I'm missing here?

Here's my code:

#!/usr/bin/perl use strict; use warnings; use Net::Telnet (); my $t = new Net::Telnet (Timeout => 10, Prompt => '/host\@user ~\/'); my ($username, $passwd) = qw(user password); $t->open("host"); $t->login($username, $passwd); my @distro = $t->cmd(String => "cat /etc/redhat-release", cmd_remove_mode => '1'); print "@distro";
Which produces this output:
$ ./connect.pl $ cat /etc/redhat-release CentOS release 5 (Final)

and I don't want the cat command. Can anyone shed some light?

UPDATE:
Set cmd_remove_mode to the number of lines to remove such as

my @distro = $t->cmd(String => "cat /etc/redhat-release", cmd_remove_mode => '2');

to remove two lines.

Comment on Can't get cmd_remove_mode to work in Net::Telnet
Select or Download Code
Re: Can't get cmd_remove_mode to work in Net::Telnet
by vek (Prior) on Jan 15, 2009 at 21:38 UTC

    According to the docs, $t->cmd_remove_mode(1) removes one line to be echoed. Is it possible the remote host is returning 2 lines, the first of which you are correctly suppressing but the second line is still echoed? Could you try setting cmd_remove_mode to 2 to see if that makes any difference?

    $t->cmd_remove_mode(2); # so that we suppress two lines my @distro = $t->cmd(String => "cat /etc/redhat-release");

    Also, check out the possibility of using option_accept()

    -- vek --
      Thank-you! I can't believe I made such an oversight. I took the docs to mean the setting had to be -any- positive integer. I'd give you mega xp if I could. :)
        Also look at the option_accept() method, so you dont have to do this for every command. Then you can leave the echo mode on its default of 'auto'.

        ...reality must take precedence over public relations, for nature cannot be fooled. - R P Feynmann

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://736670]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (12)
As of 2014-09-19 14:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (140 votes), past polls