我的主机中有一个文件说 /a/b/c/file 。我想在 dest 目录中的远程主机上创建一个文件。现在的问题是,如何使用 perl 脚本和 ssh 在远程主机中创建一个文件为 /dest/a/b/c/d/file。知道如何在脚本中创建目录。?
谢谢。
我的主机中有一个文件说 /a/b/c/file 。我想在 dest 目录中的远程主机上创建一个文件。现在的问题是,如何使用 perl 脚本和 ssh 在远程主机中创建一个文件为 /dest/a/b/c/d/file。知道如何在脚本中创建目录。?
谢谢。
要重现目录结构,请使用模块中的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 = <ERRORS>;
warn $errors if $errors =~ /\S/;
close ERRORS or warn "$0: close: $!";
print <RECV>;
close RECV or warn "$0: close: $!";
样品运行:
$ ./创建文件 746308829575e17c3331bbcb00c0898b /tmp/host/a/b/c/file 746308829575e17c3331bbcb00c0898b /tmp/dest/a/b/c/file