4

Check to see if remote directory exists using Perl Net::SSH::Perl

I want to check if a directory exists on a report server using the Net::SSH::Perl module.

This command works:

my($stdout, $stderr, $exit) = $ssh->cmd("ls -al /home/mydir");

But how can I do like:

if ( ! -e "/home/mydir/mydir_1" ) {
   mkdir "/home/mydir/mydir_1";
}

Any way to do this using this Perl module?

1
  • If you have perl on the remote end as well, just use $ssh->cmd(qq{perl -wle 'exit -e shift' '$filename'}) and then look at the exit code Commented Jan 15, 2019 at 15:46

1 Answer 1

4

The -p option for mkdir accomplishes what you want (no error if existing), so something like:

my($stdout, $stderr, $exit) = $ssh->cmd("mkdir -p /home/mydir/mydir_1");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.