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

I tried to login one server, execute the command "who" on it, and print out the result.
# !/usr/bin/perl use Net::Telnet; $t=new Net::Telnet(Timeout=>30, Errmode=>'die'); $t->open('myhostname'); print "ssssssss\n"; $t->waitfor('/login: $/i'); $t->print('myusername'); $t->waitfor('/password: $/i'); $t->print('mypasswd'); print "hhhhhhhh\n"; my @myoutput=$t->cmd("who"); print @myoutput; print "ttttttt\n";
This program could print out the "hhhhhhh" line. But it did not execute the command "who". And I did not figure out why. Could someone help me with it? Thanks a lot. PS: I tried to use $t->login('myusername','mypasswd'). It did not work either.

Replies are listed 'Best First'.
Re: how to login one host and execute commands on it?
by glide (Pilgrim) on Jan 11, 2008 at 17:26 UTC
    Hi,

    try to debug the telnet session using the input_log. After you create the Net::Telnet object do

    $t->input_log(q{net_telnet_input.log});
    run again the script and then look to the created log file.
      Thanks. It works.
Re: how to login one host and execute commands on it?
by NetWallah (Canon) on Jan 11, 2008 at 17:40 UTC
    • Since you have not set the PROMPT, it defaults to /[\$%#>\] $/. Does this match your server ? What kind of server is it anyway ?
    • Use the Dump_log option to debug the traffic.
    • On your cmd statements, set your errmode to die or add code for error handling.

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

      Solaris I am using. I could get the "who" information in the log file. But I still don't know why I could not print it out .
Re: how to login one host and execute commands on it?
by zentara (Cardinal) on Jan 12, 2008 at 14:29 UTC
    Just a comment on security. Unless you are absolutely sure that you are on a safe internal network, using telnet is generally frowned upon. Play it safe and use Net::SSH2 instead, and your future will be better.
    #!/usr/bin/perl use warnings; use strict; use Net::SSH2; # assuming a user named 'z' for demonstration # connecting to localhost, so you need your sshd running # see maillist archives at # http://lists.sourceforge.net/lists/listinfo/ssh-sftp-perl-users # for deeper discussions my $ssh2 = Net::SSH2->new(); $ssh2->connect('localhost') or die "Unable to connect Host $@ \n"; $ssh2->auth_password('z','ztester') or die "Unable to login $@ \n"; #shell use my $chan = $ssh2->channel(); $chan->blocking(0); $chan->shell(); print $chan "who\n"; print "LINE : $_" while <$chan>; $chan->close; __END__

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum