getting output of telnet from perl

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

i had written  code to connect to unix server from Windows xp(telnet)
where i installed perl.
i  have to do with telnet. I think I am able to connect with unix
server. but i am not able to get out put from the server. i have
triedwith 'ls' and 'who' commands. But i am not able to getting any
output from server.
The code is :

use Net::Telnet;

# instantiate a new CGI object
my $telnet = new Net::Telnet(Timeout => 10,
                     Errmode => 'die');

$telnet->open("servername") or die "hai $telnet->errmsg ";
print "connected";
$telnet->waitfor('/login: $/i');
$telnet->print("ixlourd") or  die $telnet->errmsg;
$telnet->waitfor('/password: $/i');
$telnet->print("qwest1") or  die $telnet->errmsg;
print "logged in";
$telnet->waitfor('/ixlourd\@e2epia2: $/');
print "Before ls";
 $telnet->waitfor('/ixlourd\@e2epia2: $/');
 #$output = $telnet->waitfor('/$ $/i');
 print $output;
@lines=$telnet->print("ls") or die $telnet->errmsg;

print @lines;

"connectedlogged inBefore ls".

I have copied module from CPAN to perl directory. why it is
not giving any output of 'ls' . it is simply it is giving
"connectedlogged inBefore ls"
 How can i make check whether it is connected to my unix server

Re: getting output of telnet from perl wrote:

Quoted text here. Click to load it

You seem to have forgotten:
use strict;
use warnings;

Quoted text here. Click to load it

errmsg() is a method, not a variable, and so does not interpolate.  If
this die() were ever executed, you'd get something like:
hai Net::Telnet=HASH(0x123456)->errmsg

Quoted text here. Click to load it

What do you think this is doing?

Have you read the documentation for the module you're using, and the
specific documentation for the method you're using in that module?

perldoc Net::Telnet
     print - write to object
             $ok = $obj->print(@list);

         This method writes @list followed by the
         output_record_separator to the open object and returns
         "1" if all data was successfully written.

Quoted text here. Click to load it

That is not the way you install a Perl module

perldoc perlmodinstall

Quoted text here. Click to load it

because print() does not return the results of the command it sent to
the server.

Read the documentation for the module to learn what method you should
be using instead....

Quoted text here. Click to load it

You're already doing that, by specifying the errmode() to 'die'

Paul Lalli

Re: getting output of telnet from perl

Thanx for your comments/suggetions.' use warnings' statement showing
all warnings its ok.
 But i am still having problems.. I dont know how to do correctly.
this is remodified code.
i am getting  timed out errors. the following program as

use strict;
use warnings;

use Net::Telnet();

my $telnet;my $msg;
my @lines;# = new  NET::Telnet;
$telnet = new Net::Telnet(Timeout=>40,Errmode=>'die');

if (! defined $telnet) {
                die "Unable to create telnet object ";
    else {
           print "\ndefined and created telnet";


 if ( $msg = $telnet->errmsg) {
                die "Unable to open telnet to   $msg";
        } else {
                 print "\nconnected to server";

sleep (20);

#$telnet->waitfor('/login: ?$/');

sleep (10);   #insted of waitfor..??
if ($msg = $telnet->errmsg) {
                die "Unable to login to   $msg  ";
        }else {
    print "\nlogged in"; }

my @cmdOutput = $telnet->cmd("who");

print @cmdOutput;

errormessages i am getting successfully now. Manually i connected
telnet it is very fast.
 I am using sleep(some time) insted of waitfor>> will it work..? i am
getting  "connected to server" means i am getting connected to server.
but not able to print command at last line of the code is
 output of above code is  like this

defined and created telnet
timed-out waiting for command prompt at line 26
connected to server

--Thankx a lot

Site Timeline