Perl - 如何将二进制(映像)文件从“主”主机发送到远程主机并创建目录结构?

问题描述:

我已经做了一堆阅读和一些测试无济于事。Perl - 如何将二进制(映像)文件从“主”主机发送到远程主机并创建目录结构?

我发现这里的计算器,其指导我在正确的方向,但是这个剧本,我的能力是不够的修改并完全理解我的需要。

#! /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: $!"; 

场景:

我有“主”主机例如在目录中的图像文件/home/user/public_html/images/red/id100/image01.jpg (thru image012.jpg)

我想复制/它们发送到我的远程主机建立和红/ ID100路径如果!-d。 (以下简称“/图片”文件夹中存在前)

我需要一个用户名和密码发送到远程主机,以及中得到的。这是一个典型的cPanel共享服务器环境。

我的主服务器专用,也安装了cpanel管理。

我看着NET :: FTP,文件::远程,网络:: Telnet和网:: SFTP,但没有可用的例子即是 “简单化” 下调至我的水平。

我将最终需要与ASCII文件做到这一点太多,但我相信一旦我得到它与图像的工作,我可以找出xfermode开关,我希望。

感谢您的帮助和详细的例子。我总是在这里学到很棒的东西。

使用SFTP它应该是相当简单:

use Net::SFTP::Foreign; 
my $sftp = Net::SFTP::Foreign->new($HOST, passwd => $PASSWD); 
$sftp->error and die "Unable to connect to remote host: " . $sftp->error; 
$sftp->rput($SRC_BASE, $DST_BASE); 

...或者使用ssh + rsync的...

use Net::OpenSSH; 
my $ssh = Net::OpenSSH->new($HOST, passwd => $PASSWD); 
$ssh->error and die "Unable to connect to remote host: " . $ssh->error; 
$ssh->rsync_put({recursive => 1}, $SRC_BASE, $DST_BASE) 
    or die "rsync failed: " . $ssh->error; 
+1

谢谢。意志也创建目录,如果它不存在? – DulcimerDude 2011-01-14 21:27:12