getting output of telnet from perl

getting output of telnet from perl

am 06.04.2006 12:03:21 von kishore.konjeti

hi
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->print('who');
$telnet->waitfor('/ixlourd\@e2epia2: $/');
#$output = $telnet->waitfor('/\$ $/i');
print $output;
@lines=$telnet->print("ls") or die $telnet->errmsg;


print @lines;


OUTPUT:
"connectedlogged inBefore ls".


I have copied telnet.pm 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
successfully.
Thanks,
kishore

Re: getting output of telnet from perl

am 06.04.2006 16:59:57 von Paul Lalli

kishore.konjeti@gmail.com wrote:

> 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 :
>

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

> 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 ";

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

> 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->print('who');
> $telnet->waitfor('/ixlourd\@e2epia2: $/');
> #$output = $telnet->waitfor('/\$ $/i');
> print $output;
> @lines=$telnet->print("ls") or die $telnet->errmsg;

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.

> I have copied telnet.pm module from CPAN to perl directory.

That is not the way you install a Perl module

perldoc perlmodinstall

> why it is not giving any output of 'ls' . it is simply it is giving
> "connectedlogged inBefore ls"

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....

> How can i make check whether it is connected to my unix server
> successfully.

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

Paul Lalli

Re: getting output of telnet from perl

am 12.04.2006 19:03:01 von kishore.konjeti

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";
}

$telnet->open("e2epia2.uswc.uswest.com");

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

sleep (20);

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

$telnet->login("ixlourd","qwest1");
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 login...is print command at last line of the code is
ok..?
output of above code is like this
*****************************
C:\perl\perl tel.pl

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


********************
--Thankx a lot