Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: checking a directory in a remote system using perl

601 views
Skip to first unread message

Chas. Owens

unread,
Jul 14, 2010, 7:57:34 PM7/14/10
to Sooraj S, begi...@perl.org
On Wed, Jul 14, 2010 at 10:16, Sooraj S <soorajspa...@gmail.com> wrote:
>  Hi I am very new to perl. I want to login to a remote machine and
> check a directory exists or not.
>
> my code:
> ========
>
> using Net::Telnet;
>
> $t = new Net::Telnet();
> $t->open($remote_system);
> $t->login($username, $passwd);
>
> # this doesnot work.
> if (-d $my_dir) { print "ready to go"; }
>
> $t->close();
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
like this:

#!/usr/bin/perl

use strict;
use warnings;

my $host = shift;
my $dir = shift;

system "ssh", "-q", $host, "test", "-d", $dir;
my $rc = $? >> 8;
if ($rc == 0) {
print "$dir is a directory on $host\n";
} else {
print "$dir is not a directory on $host\n";
}


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

Sooraj S

unread,
Jul 14, 2010, 10:16:26 AM7/14/10
to begi...@perl.org
Hi I am very new to perl. I want to login to a remote machine and
check a directory exists or not.

my code:
========

using Net::Telnet;

$t = new Net::Telnet();
$t->open($remote_system);
$t->login($username, $passwd);

# this doesnot work.
if (-d $my_dir) { print "ready to go"; }

$t->close();

=========

Pls help me.

Sooraj S

unread,
Jul 15, 2010, 6:12:50 AM7/15/10
to begi...@perl.org
On Jul 15, 4:57 am, chas.ow...@gmail.com ("Chas. Owens") wrote:
> The most important skill a programmer can have is the ability to read.- Hide quoted text -
>
> - Show quoted text -

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

1. Using Net::Telnet module, can u suggest any way so that my above
given script will work.
2. If i use "ssh" as you suggest can i send password also through the
script..? if so how?
3. can u pls make me clear on this line - "my $rc = $? >> 8;"

Chas. Owens

unread,
Jul 15, 2010, 12:59:23 PM7/15/10
to Sooraj S, begi...@perl.org
On Thu, Jul 15, 2010 at 06:12, Sooraj S <soorajspa...@gmail.com> 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.

Jim Gibson

unread,
Jul 15, 2010, 12:47:29 PM7/15/10
to begi...@perl.org
> Hi,
> 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.
>
> 1. Using Net::Telnet module, can u suggest any way so that my above
> given script will work.

Check out the Expect category of programs and modules. These are designed to
allow you to interact programmatically with an external program or system
that expects to be interacting with a person. See, for example, Expect.pm,
Net::SSH::Expect, etc.

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

See Net::SSH::Expect. While it can be a pain, setting up a key system for
ssh means you do not have to enter a password or put a password in your
program file for all to see.

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

The system function returns a value in the variable $? to indicate the
return status of the function it called. See 'perldoc -f system' for
details.

C.DeRykus

unread,
Jul 15, 2010, 3:37:41 PM7/15/10
to begi...@perl.org

See the 'cmd' method in Net::Telnet docs: perldoc Net::Telnet.
Net::Telnet can still useful in changing passwords for instance
but only over ssh. Net::Telnet even provides a passwd change
example over ssh.

But, again, you should definitely be using ssh. There are other
modules which can facilitate this such as Net::SSH2, the earlier
mentioned Net::SSH::Expect, and Net::SSH::Perl. Check CPAN.

Warning:: Net::SSH::Perl isn't that easy to build IMO. (Or at least
that used to be the case.) You might want to stick to Net::SSH2.

--
Charles DeRykus

0 new messages