簡體   English   中英

在一個Net :: SFTP :: Foreign會話中上載多個文件

[英]To upload multiple files in one Net::SFTP::Foreign session in perl

我正在嘗試使用Net :: SFTP :: Foreign模塊將一組文件上傳到perl中的遠程計算機。 文件名存儲在一個文本文件中。但是所有文件都沒有上載。 僅上傳一個文件。

#!/usr/local/roadm/bin/perl
# This is compiled with threading support

use strict;
use warnings;
use threads;
use threads::shared;
use Net::SFTP::Foreign;
my $count=0;
my %args = (
user     => 'root',
password => 'Ht5h10N2',
more     => '-v',
autodisconnect => 0
);           
print "Starting main program\n";
open(fa ,"<file_list.txt");
my @con =<fa>;
close fa;
my $sftp = Net::SFTP::Foreign->new('hadoop-dev2', %args); 
foreach  (@con)
{
chomp $_;
$sftp->put($_,"$_");

}

您永遠不會檢查各種操作的狀態! 您最初創建$sftp構造函數是否起作用? 您打開的那個文件真的打開了嗎? 該文件在遠程系統上是否存在?

您必須始終在Perl中檢查命令的狀態!

use strict;
use warnings;
use feature qw(say);
use File::Basename;

use threads;
use threads::shared;
use Net::SFTP::Foreign;

my %args = (
    user     => 'root',
    password => 'Ht5h10N2',
    more     => '-v',
    autodisconnect => 0
);           

# Where is `%args` coming from?
my $sftp = Net::SFTP::Foreign->new('hadoop-dev2', %args);   # Check whether succeeded or failed!
if ( $sftp->error ) {
    die qq(Could not establish the SFTP connection);
}

say "Starting main program";

open my $fh, "<", "file_list.txt"   # Check whether succeeded or failed!
    or die qq(Could not open file "file_list.txt");
}

while ( my $file = <$fh> ) {
    chomp $file;
    $sftp->put( $file, $file ) );   # Check whether succeeded or failed!
    if ( $sftp->error ) {
        warn qq(Could not download file "$file");
        my $remote_files_ref = $sftp->ls();  # Check whether succeeded or failed!
        if ( $sftp->error ) {
            warn qq(Cannot get stat or remote directory.);
        }
        else {
            say qq(List of files in "$remote_dir":);
            for my $remote_file ( @{ $remote_files_ref } ) {
                say "    $remote_file";
            }
        }
    }
}

注意,我檢查open是否有效, $sftp的構造函數是否有效,以及每次使用Net::SFTP::Foreign 例如,我無法下載不存在的文件。 也許它不存在,因此我執行$sftp->ls來查看它何時不起作用。

您可以使用autodie這是一個編譯用於在Perl各種文件的命令,並且是一個設置,您可以使用Net::SFTP::Foreign Autodie很不錯,因為它會在出現錯誤時自動殺死程序,從而使perl成為更多基於異常的語言。 這樣,如果出現錯誤,而您沒有發現它,則程序將死亡。

如果您不希望程序徹底失敗,則可以使用eval來測試某項是否有效:

$sftp->Net::SFTP::Foreign( yadda, yadda, { autodie => 1} ); #Autodie is now turned on:

eval {   # Checks whether the file exists
   $sftp->get( $file, $file );
}
if ( $@ ) {
   warn qq(ERROR: File "$file" is not found!);
} else {
   say qq(Downloaded "$file".);
}

回復

沒有錯誤,我運行了您的代碼,但是我仍然無法上傳:(任何幫助,我們將不勝感激

因此,您是說$sftp->get不會下載文件,但都不會設置$sftp->error嗎?

我在代碼中的一些地方看到返回了undef ,但是沒有調用sftp->_set_error 讓我們看看$sftp->get返回trueundef 根據源代碼,這就是應該做的。 如果是undef,我們將假設它失敗。

while ( my $file = <$fh> ) {
    chomp $file;
    if ( not $sftp->put( $file, $file ) ) {  # Check whether succeeded or failed!
        warn qq(Could not download file "$file");
        my $remote_files_ref;
        if ( $remote_files_ref = $sftp->ls() ) {  # Check whether succeeded or failed!
            warn qq(Cannot get stat or remote directory.);
        }
        else {
            say qq(List of files in "$remote_dir":);
            for my $remote_file ( @{ $remote_files_ref } ) {
                say "    $remote_file";
            }
        }
    }
}

我敢打賭,問題在於您的file_list.txt有一些文件的完整路徑,並且FTP服務器上不存在這些文件的路徑。 請記住,FTP不會為您創建目錄,因此如果您有

/etc/passwd

在您的file_list.txt ,最好有一個名為

/etc

在您的FTP服務器上。

問題是,該代碼正在cygwin上在Windows上運行,因此主要錯誤之一是與單個VTY資源的線程競爭以及用於獲取TIMED OUT的線程,因此我使用Expect腳本解決了該問題。

暫無
暫無

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

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