2014-01-23 44 views
1

下面我的Perl腳本是非常基本的。它會複製位於一臺服務器上的.zip文件並將其傳輸到另一臺服務器。設置二進制傳輸模式

#!/usr/bin/perl -w 

use strict; 
use warnings; 
my $remotehost ="XXXXXX"; 
my $remotepath = "/USA/Fusion_Keyword_Reports"; 
my $remoteuser = "XXXXXXX"; 
my $remotepass = "XXXXXXX"; 

my $inputfile ="/fs/fs01/crmdata/SYWR/AAM/list8.txt"; 
my $remotefile1; 
#my $DIR="/fs/fs01/crmdata/SYWR/AAM"; 
open (FILEIN, "<", $inputfile) or die "can't open list8 file"; 


while (my $line =<FILEIN>) { 
if ($line =~ m /Keywords-Report(.*?)/i && $line !~ m/Keywords-Report-loopback/i) { 
    print $line; 
$remotefile1 =$line; 
last; 
} 
} 
close FILEIN; 

print "remotefile $remotefile1\n"; 


my $DIR1="/fs/fs01/crmdata/SYWR/AAM/$remotefile1"; 

my $cmd= "ftp -in"; 


my $ftp_command = "open $remotehost 
        user $remoteuser $remotepass 
        cd $remotepath 
        asc 
        get $remotefile1 
        bye 
       "; 

    open (CMD, "|$cmd"); 
    print CMD $ftp_command; 
close (CMD); 


exit(0); 

當我運行該腳本它確實工作,但我得到一個錯誤,並且傳輸的文件因此被損壞。

226 Transfer complete. 
WARNING! 40682 bare linefeeds received in ASCII mode. 
File may not have transferred correctly. 

我做了一些閱讀,我想我需要設置傳輸模式爲二進制。但是我真的不確定如何在腳本中做到這一點。另外,我不確定這是否是正確的解決方案。

我真的很感激你對這個錯誤的想法。如果將傳輸模式設置爲二進制將解決此問題,請你告訴我我會在哪裏做到這一點?

+0

我沒有訪問手冊頁的權限,但是根據過去'ftp'的使用經驗,將您的參數'asc'改爲'bin'可能會做到。如果不是,請嘗試一個簡單的'b'。祝你好運。 – shellter

+2

Shellter是對的。不要將模式設置爲ascii(移除'asc'行)是一個很好的開始,設置二進制文件(將'asc'更改爲'bin')甚至更好。另外,請考慮使用Net :: FTP模塊(http://perldoc.perl.org/Net/FTP.html),它將提供更好的錯誤處理。 –

+0

只需將asc更改爲二進制拼寫就可以完全修復它!感謝Guntram的幫助! – user2748540

回答

2
my $ftp_command = "open $remotehost 
       user $remoteuser $remotepass 
       cd $remotepath 
       binary 
       get $remotefile1 
       bye 
      ";