'ftp'에 해당되는 글 1건

  1. 2008/05/09 perl의 Net::FTP 모듈을 이용한 ftp 전송

#!/usr/bin/perl -w

#############################################################
# 파일명 : E:\perl\test\daum\daum_ftp.pl
# 작성자 : 고기스님 (gogisnim@gmail.com)
# 작성일 : 2008.05.09
# 사용법 : perl E:\perl\test\daum_ftp.pl [all|new|del]
# 설명    : daum 검색 데이터를 ftp로 제공한다.
#             스케줄에 등록하고 매일 새벽 자동실행된다.
# 주의    : new 일때 실행후 관련 파일은 자동 삭제된다.
#             따라서 del 옵션은 실제로는 필요가 없다.
#############################################################

use strict;
use warnings;
use Net::FTP;

my $ftp;
my $host = "myhost.test.com";
my $port = "2121";
my $user = "someuser";
my $pwd = "somepassword";
my $local_dir = "E:\\inetpub\\test\\daum\\";
my $target_dir;
my $fh;

my @real_files;


##### 파라미터 갯수를 체크하고 옵션(all, new, del) 값을 저장 #####
die "ARGV failed !!!" unless $#ARGV == 0;
my ( $opt ) = @ARGV;

##### all 일 경우 #####
if ( $opt eq "all" ) {
# 업로드할 파일 목록을 배열에 저장한다.
push @real_files, $local_dir.$opt."\\static.source.0";

##### new 또는 del 일 경우 #####
} else {
$target_dir = $local_dir.$opt;
opendir $fh, $target_dir
or die "file handle failed: $! \n";
while ( my $tmp_f = readdir $fh ) {
if ( ! -d $tmp_f ) { # 디렉토리는 포함하지 않는다.
  # 업로드할 파일 목록을 배열에 저장한다.
  push @real_files, $local_dir.$opt."\\".$tmp_f;
}
}
closedir $fh;
}

##### ftp 커넥션 #####
$ftp = Net::FTP->new($host, Port=>$port, Debug=>0)
or die "Cannot connect host $host : $@";


##### ftp 사용자 로그인 #####
$ftp->login($user, $pwd)
or die "cannot login ", $ftp->message;


##### 윈도우즈에서는 텍스트파일의 라인 마지막 1 byte가 짤렸다. #####
##### 이를 해결하기위해 binary 메소드를 호출한다.    #####
##### 리눅스/유닉스에서는 없어도 정상 작동한다.     #####
$ftp->binary;


##### 파일 전송 #####
foreach my $f ( @real_files ) {
$ftp->put($f)
or die "put failed ", $ftp->message;
}


##### ftp 커넥션 종료 #####
$ftp->quit;


##### new 또는 del 일 경우 로컬파일을 삭제한다 #####
if ( $opt ne "all" ) {
foreach my $del_f ( @real_files ) {
unlink($del_f);
}
}


binary

Transfer file in binary mode. No transformation will be done.

Hint: If both server and client machines use the same line ending for text files, then it will be faster to transfer all files in binary mode.


http://search.cpan.org/~gbarr/libnet-1.22/Net/FTP.pm