簡體   English   中英

Perl-如何將二進制(圖像)文件從“主”主機發送到遠程主機並創建目錄結構?

[英]Perl - How to send a binary (image) file to a remote host from “master” host and create directory structure too?

我做了很多閱讀和一些測試都沒有用。

我在stackoverflow上找到了此腳本,該腳本向正確的方向提供了指導,但是我的能力不足以滿足我的需要進行修改和充分理解。

#! /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)

我想將它們復制/發送到我的遠程主機,如果使用-d則創建red / id100路徑。 (“ / images”文件夾之前存在。)

我也需要向遠程主機發送用戶名和密碼才能進入。這是典型的cpanel共享服務器環境。

我的主服務器專用,還安裝了cpanel管理。

我查看了NET :: FTP,File :: Remote,Net :: Telnet和Net :: 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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM