Click here to get back home

getting output of telnet from perl

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
getting output of telnet from perl kishore.konjeti 04-06-2006
Posted by kishore.konjeti on April 6, 2006, 6:03 am
Please log in for more thread options


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


Posted by Paul Lalli on April 6, 2006, 10:59 am
Please log in for more thread options


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


Posted by kishore.konjeti on April 12, 2006, 1:03 pm
Please log in for more thread options


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


Similar ThreadsPosted
getting output of telnet from perl script April 10, 2006, 5:35 am
Template-Toolkit: Flushing Output or Incremental Output, how? September 13, 2007, 2:45 pm
How to handle input and output of a program executed by System function in perl July 18, 2004, 2:31 pm
Telnet from perl September 23, 2004, 2:12 pm
Net::Telnet not working with perl 5.8.5 November 15, 2005, 2:48 am
problem in telnet using perl December 27, 2007, 11:22 pm
Using Net::Telnet to connect to Perl server January 22, 2005, 12:57 am
How do you telnet from 1 host to another using Telnet Module December 21, 2005, 4:15 pm
net::telnet to ms telnet server May 19, 2005, 12:28 am
output-monitoring module April 6, 2005, 11:38 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap