How to create a file on a remote host along with creating a directory using ssh

I have a say / a / b / c / file file on my host. I want to create a file on a remote node in the say dest directory. Now the question arises: how to create a file on a remote host as / dest / a / b / c / d / file using perl script and using ssh. Any idea how to create directories in a script.?

Thank.

+3
source share
1 answer

To reproduce the directory structure, use catfilealso abs2relfrom the module File::Spec: catfilecombines fragments into the enter path, and abs2relsets the path relative to some base directory.

File::Copy copy . , sshopen3 , .

:

  • mkdir -p $dst_dir, , .
  • cat >$dst_file, SEND
  • md5sum $dst_file, ,

:

#! /usr/bin/perl

use warnings;
use strict;

use File::Basename;
use File::Copy;
use File::Spec::Functions qw/ abs2rel catfile /;
use Net::SSH qw/ sshopen3 /;

my $HOST     = "user\@host.com";
my $SRC_BASE = "/tmp/host";
my $SRC_FILE = "$SRC_BASE/a/b/c/file";
my $DST_BASE = "/tmp/dest";
system("md5sum", $SRC_FILE) == 0 or exit 1;

my $dst_file = catfile $DST_BASE, abs2rel $SRC_FILE, $SRC_BASE;
my $dst_dir  = dirname $dst_file;
sshopen3 $HOST, *SEND, *RECV, *ERRORS,
         "mkdir -p $dst_dir && cat >$dst_file && md5sum $dst_file"
  or die "$0: ssh: $!";

binmode SEND;
copy $SRC_FILE, \*SEND or die  "$0: copy failed: $!";
close SEND             or warn "$0: close: $!";  # later reads hang without this

undef $/;
my $errors = <ERRORS>;
warn $errors if $errors =~ /\S/;
close ERRORS or warn "$0: close: $!";

print <RECV>;
close RECV or warn "$0: close: $!";

:

$ ./create-file
746308829575e17c3331bbcb00c0898b  /tmp/host/a/b/c/file
746308829575e17c3331bbcb00c0898b  /tmp/dest/a/b/c/file
+2

Source: https://habr.com/ru/post/1752327/