mnlight has asked for the wisdom of the Perl Monks concerning the following question:

there must be a way to make a directory path on a remote server. Can anyone tell me what the best way to do this is. I have tried using Telnet and that does not work consistently enough. I am now trying to simplify it and use rsh but it does not seem to want to build the path. I would also like to be able to check if the path exists before I create it but rsh will not let me do that.
my $dir=sprintf("%s/%s/%s/%s/%s", $params->get("Backup.Root.Dir"), $server, $params->get("Server.Name"), $cltdb, $type); my $remotecmd = `rsh $server mkdir -p $dir` or $app->error($FATAL,"Can't make directory [ $dir ]::$DBI::errstr +");

Replies are listed 'Best First'.
(jeffa) Re: Creating path on remote host
by jeffa (Bishop) on Feb 19, 2003 at 15:25 UTC
    I wouldn't do anything like that without going through ssh, personally. Have you tried Net::SSH::Perl yet?
    use Net::SSH::Perl; my $ssh = Net::SSH::Perl->new('your.host.com'); $ssh->login($login, $pass); my ($stdout, $stderr, $exit) = $ssh->cmd("mkdir -p $dir");

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Creating path on remote host
by steves (Curate) on Feb 19, 2003 at 15:46 UTC

    Besides being more secure, the other advantage of jeffa's solution is that it gives you standard output, standard error, and command exit status neatly packaged up. That way you can check exit status and/or standard error to see why it failed. In your example you can do something similar by redirecting standard error to a file and checking if anything ends up there, something like this:

    use strict; use POSIX qw(tmpnam); my $dir = '/foo/bar'; my $server = 'fred'; my $error_file = tmpnam(); my $remotecmd = `rsh $server mkdir -p $dir 2>$error_file`; if ((stat($error_file))[7]) { local *ERROR; open ERROR, "<$error_file" or die "Failed to ope $error_file: $!\n +"; my @lines = <ERROR>; warn "Failed to make remote directory: ", join("\n", @lines), "\n" +; } unlink($error_file);
    My experience with rsh is that it usually does not propagate back any bad exit status from the remote side. I do sometimes bypass ssh for internal connections.