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

Fellow Monks,
I want to connect remote server using Telnet module and find the hostname and print the host name.But the code is not working for me.It is giving the ouput as 1,since the command is executed successfully.But I want the host name as output.See the code below,
Use Net::TelNet; $telnet = new Net::Telnet ( Timeout=>3600,Prompt => "/$prompt/i",Errmo +de=>'die'); $telnet->open("susan"); $telnet->waitfor('/login: $/i'); $telnet->print('blab'); $telnet->waitfor('/password: $/i'); $telnet->print('blab'); $telnet->waitfor("/$prompt/i"); print $telnet->cmd("hostname"); $hostname = $telnet->cmd("hostname"); print"Host => $hostname\n"; My Expected Output :- Host => osmosun.wmjapan(i.e hostname) Current outuput => 1
Pls let me know how to get the value of the command to one variable.
Thanks.

Replies are listed 'Best First'.
Re: Telnet in perl
by shmem (Chancellor) on Jun 26, 2006 at 10:04 UTC
    Read Net::Telnet again.. ;-)

    $telnet->cmd() returns an array, which you are evaluating in scalar context (i.e. you're asking for the number of elements in the array).

    say

    @list = $telnet->cmd("hostname"); print "Host => $list[0]\n";

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Telnet in perl
by kabeldag (Hermit) on Jun 26, 2006 at 10:13 UTC
    Of course. Net::TelNet is probably 'Net::Telnet' and Use is of course 'use'.
    :-P
Re: Telnet in perl
by jesuashok (Curate) on Jun 26, 2006 at 10:07 UTC
    Hi

    Please note the modification done on your code.

    Use Net::TelNet; $telnet = new Net::Telnet ( Timeout=>3600,Prompt => "/$prompt/i",Errmo +de=>'die'); $telnet->open("susan"); $telnet->waitfor('/login: $/i'); $telnet->print('blab'); $telnet->waitfor('/password: $/i'); $telnet->print('blab'); $telnet->waitfor("/$prompt/i"); @hostname = $telnet->cmd("hostname"); print"Host Details => ", join("-",@hostname) , "\n";

    "Keep pouring your ideas"
      Hi Shmem/jesuashok,

      I tried both of yours code and got the output as follows,
      <susan@sohuswmisr:>perl 354.pl Host =>
      Expected output
      Host => susan@dohudemidt
      Pls help me monks.
      Thanks.
        Hi Ananymous Monk!

        How it will work for you.Until you give some value to the prompt it won't work at all.In the place of prompt you have to give the value what is coming when you connect manually to the remote server .

        Eg.$prompt = "\<manoj\\\@ostrich.india:\>";

        See the modified code below,
        Use Net::TelNet; $prompt = "\<manoj\\\@ostrich.india:\>"; $telnet = new Net::Telnet ( Timeout=>3600,Prompt => "/$prompt/i",Errmo +de=>'die'); $telnet->open("ostrich"); $telnet->waitfor('/login: $/i'); $telnet->print('manoj'); $telnet->waitfor('/password: $/i'); $telnet->print('manoj'); $telnet->waitfor("/$prompt/i"); print $telnet->cmd("hostname"); @hostname = $telnet->cmd("hostname"); print"Host => @hostname\n";
        See the output:-
        ostrich.india Host => ostrich.india <manoj@ostrich.india:>
        Thanks and Regards,
        madtoperl.