Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

how to use URI::telnet

Occasional Visitor

how to use URI::telnet

Hi,

I am very new to perl. I want to know how i can make use of URI::telnet module to login to a remote host.

Any kind of help would be appreciated.
3 REPLIES
Honored Contributor Honored Contributor

Re: how to use URI::telnet

I'm not a Perl guru, but as far as I understand, the URI::telnet module will only help you in splitting the "telnet://user:password@host.name.example:port/" style URL to its component parts, not for the actual connection.

See the documentation of the URI package:
http://search.cpan.org/~gaas/URI-1.54/URI.pm

For actually connecting to a Telnet server, you'll want Net::Telnet instead. See the programming examples near the end of this web page:

http://search.cpan.org/~jrogers/Net-Telnet-3.03/lib/Net/Telnet.pm

In general, you should know that there is a lot of useful documentation available at perl.org, and in the Comprehensive Perl Archive Network (CPAN):
http://www.perl.org/
http://cpan.perl.org/

MK
MK
Acclaimed Contributor Acclaimed Contributor

Re: how to use URI::telnet

Hi:

As Matti said, if you actually want to login to a remote host to perform some work, you want the 'Net::Telnet' module and not the 'URI::telnet' one. A simple example might be:

# cat ./mytelnet
#!/usr/bin/perl
use strict;
use warnings;
use Net::Telnet;
die "'Host' 'User' 'Pass' expected\n" unless @ARGV == 3;
my ( $host, $user, $pass ) = ( shift, shift, shift )
or die "Host User Pass expected\n";
my $telnet = new Net::Telnet;
$telnet->open($host);
$telnet->login( $user, $pass );
print $telnet->cmd('uname -a'), "\n"; #...print to tty directly...
$telnet->print("bdf");
my ($output) = $telnet->waitfor('/[\$%#>] $/'); #...capture first...
print $output;
$telnet->close;
print "\n\n";
1;

...run as:

# ./mytelnet myserver mylogin mypassword

Be aware that anyone looking at the process table (with 'ps' for example) would see the cleartext password in the runtime arguments. Then of course, Telnet itself isn't secure either ;-)

Regards!

...JRF...
Highlighted
Acclaimed Contributor Acclaimed Contributor

Re: how to use URI::telnet

Hi:

I should add that using a '.netrc' file helps hide the password somewhat.

# cat ./mytelnet
#!/usr/bin/perl
use strict;
use warnings;
use Net::Netrc;
use Net::Telnet;
die "'host' 'login' expected\n" unless @ARGV == 2;
my ( $host, $login ) = ( shift, shift ) or die "Host User Pass expected\n";
my ( $machine, $pass, $acct );
if ( $machine = Net::Netrc->lookup( $host, $login ) ) {
( $login, $pass, $acct ) = $machine->lpa();
}
else {
die "No matching '.netrc' entry found\n";
}
my $telnet = new Net::Telnet;
$telnet->open($host);
$telnet->login( $login, $pass );
print $telnet->cmd('uname -a'), "\n"; #...print to tty directly...
$telnet->print("bdf");
my ($output) = $telnet->waitfor('/[\$%#>] $/'); #...capture first...
print $output;
$telnet->close;
print "\n\n";
1;

...run as:

# ./mytelnet myserver mylogin

...with an appropriate '.netrc' file.

Regards!

...JRF...