Re: how to use URI::telnet (256 Views)
Reply
Occasional Visitor
anraevlus18
Posts: 1
Registered: ‎07-12-2010
Message 1 of 4 (256 Views)

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.
Please use plain text.
Honored Contributor
Matti_Kurkela
Posts: 6,271
Registered: ‎12-02-2001
Message 2 of 4 (256 Views)

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
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 4 (256 Views)

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...
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 4 (256 Views)

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...
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation