On Thu, Jul 15, 2010 at 06:12, Sooraj S <[email protected]> wrote:
snip
>> Opening a telnet connection does not mean that Perl code will run on
>> the remote system.  You should also not be using telnet as it is very
>> insecure.  Look up ssh, configure your ssh keys, and try something
snip
>  Thanks for your reply. Actually i need to login to a remote machine
> and do some series of steps including creating,editing and taring some
> files and executing some other scripts..The remote machine is in our
> local network itself..so there is no security issues and thats why i
> am using telnet. Anyway thanks for ur explanation.
snip

Internal security is as important as external security.  The machine
would not have a password if everyone who had network access to it
should be able to log into it.  By using the telent protocol you are
sending the username and password  in cleartext across the network.
This is bad.  Telnet is a dead protocol.  You should move on to ssh.

snip
> 1. Using Net::Telnet module, can u suggest any way so that my above
> given script will work.
snip

This may work, but I can't find a telnet server to test it against.
The trick is that you are running shell commands in the telnet
connection, not Perl.  So, to find out if a directory exists you can
say

test -d directory_name; echo $?

which will print 0 (if the directory exists) or 1 (if it doesn't).
The cmd method will return whatever output is printed by the shell
command.

#!/usr/bin/perl

use strict;
use warnings;

use Net::Telnet;

sub dir_exists {
        my ($remote, $dir) = @_;

        my $rc = $remote->cmd("test -d $dir; echo \$?");
        warn $rc;

        return $rc ? 0 : 1;
}

my $host     = "the hostname you want to connect to";
my $user     = "your user";
my $password = "your password";

my $remote = Net::Telnet->new(
        Timeout => 30,
);

$remote->open($host);
$remote->login(
        Name     => $user,
        Password => $password,
);

print "/tmp is ", dir_exists($remote, "/tmp") ? "" : "not ", "a dir\n";
print "/foo is ", dir_exists($remote, "/foo") ? "" : "not ", "a dir\n";



snip
> 2. If i use "ssh" as you suggest can i send password also through the
> script..? if so how?
snip

That is what passwordless keys are for.  Never use a password with
ssh.  Always use encrypted keys for people and passwordless keys or
ssh-agent (to store the unencrypted key in memory) for automated
tasks.

snip
> 3. can u pls make me clear on this line - "my $rc = $? >> 8;"
snip

The exit code from system is put into $?.  It is a two byte record
that contains the exec return value and the actual exit code of the
running program.  To get just the byte that contains the actual exit
code of the running program you must shift it eight bits to the right.
 The ssh command passes along the exit code of the program it invokes,
so, in this case, it returns test's exit code.  The test command in
the shell returns an exit code of 0 if the test succeeds and 1 if it
fails.


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to