要重现的目录结构中,使用catfile和abs2rel从File::Spec模块:catfile联接件,使一个路径,并且abs2rel给出相对于一些基本目录的路径。
File::Copy模块的copy将复制到句柄。这很适合于sshopen3如何在目标端打开标准输入,输出和错误的句柄。
远程命令具有3个部分:
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 = ;
warn $errors if $errors =~ /\S/;
close ERRORS or warn "$0: close: $!";
print ;
close RECV or warn "$0: close: $!";
采样运行:
$ ./create-file
746308829575e17c3331bbcb00c0898b /tmp/host/a/b/c/file
746308829575e17c3331bbcb00c0898b /tmp/dest/a/b/c/file