티스토리 뷰

Developer

[ Perl ] FTP Upload

rocksea 2012. 9. 28. 09:48

FTP batch 작업을 위한 Perl Script ( 참고용 )

#!/usr/bin/perl
use Net::FTP;
use Cwd;
`rm -rf *.jpg* *error.ht*`;
`cd /path-to-dir/`;
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
sub writeLog() {
        my ($msg)       = @_;
        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
        my $yyyymmdd = sprintf("%04d-%02d-%02d", $year + 1900, $mon +1, $mday);

        my $logMsgFormat        = $yyyymmdd."\t".$hour.':'.$min.':'.$sec."\t";

        # ÆÄÀÏÀÌ Á¸ÀçÇÏÁö ¾ÊÀ¸¸é »ý¼º
        if (-e '/home/was/mozen/log/applog_'. $yyyymmdd .'.log')
        {
                open(createFileHandle, ">/path-to-dir/log/applog_". $yyyymmdd .".log");
                        print createFileHandle '';
                close(createFileHandle);
        }

        $msg    =~ s/(\s){1,}$//;
        $msg    =~ s/\n/\n$logMsgFormat/;

        open(writeFileHandle, ">>/path-to-dir/log/applog_". $yyyymmdd .".log");
                print writeFileHandle $logMsgFormat . $msg ."\n";
        close(writeFileHandle);
}
sub putFTP() {
        my $hostname    = "192.168.0.200";
        my $user        = "media";
        my $passwd      = "media";
        my @transferFileList = glob "test*.jpg";

        &{$funcname}("TEST ROCKSEA!!!");

        &{$funcname}("FTP Connect");
        $ftp = Net::FTP->new($hostname, Debug => 0) or &{$funcname}("Cannot connect to ".$hostname.": ".$@);
        $ftp->login($user,$passwd) or &{$funcname}("Cannot login ". $ftp->message);
        $ftp->binary();
        $ftp->cwd("/path-to-dir/") or &{$funcname}("Cannot change working directory ". $ftp->message);
        for (@transferFileList){
                print cwd . "/$_\n";
                #$ftp->put("cctv*.jpg") or &{$funcname}("put failed ". $ftp->message);
                $ftp->put(cwd . "/$_","./path-to-dir/$_") or &{$funcname}("put failed ". $ftp->message);
        }
        $ftp->quit;
        &{$funcname}("FTP Disconnected");
}
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

our $funcname = "writeLog";

&{$funcname}("===================== mozen cctv start ======================");


local @getImageList     = (
                                1,      2,      3,      4,      5,      6,      7,      8,      9,      10,
                                11,     12,     13,     14,     15,     16,     17,     18,     19,     20,
                                21,     22,     24,     25,     26,     27,     28,     29,     30,     31,
                                32,     35,     36,     37,     38,     39,     40,     41,     42,     43,
                                44,     45,     46,     47,     49,     50,     51,     52,     53,     54,
                                55,     56,     57,     58,     59,     60,     61,     62,     63,     64,
                                65,     66,     67,     68,     69,     70,     71,     72,     73,     74,
                                75,     76,     77,     78,     79,     80,     81,     82,     83,     84,
                                85,     86,     88,     89,     90,     93,     95,     96,     97,     101,
                                102,    103,    104,    105,    106,    107,    108,    109,    110,    111,
                                112,    113,    115,    116,    117,    119,    120,    121,    122,    123,
                                124,    125
                        );


local @result   = ();
local $getImagePath     = "http://iportal.mozen.co.kr/wireless/img/cctv/a_buffer/";

#for (my $i = 0; $i <= 1; $i++)
for (my $i = 0; $i <= $#getImageList; $i++)
{
        my $fullPath    = $getImagePath .'cctv'. $getImageList[$i] .'.jpg';
        &{$funcname}("get ". $fullPath);
        $result[$i]     = `wget $fullPath`;
        &{$funcname}("result ". $result[$i]);
}

#for (;;)
#{
#       for (my $i = 0; $i <= $#getImageList; $i++)
#       {
#               if (-e '/home/was/cctv'.$getImageList[$i].'.jpg')       { $cntImage++; }
#               else{ $errorCount++; }
#       }
#
#       sleep(60);
#}
putFTP();
`rm -rf *.jpg* *error.ht*`;
&{$funcname}("===================== mozen cctv end ======================");
exit(0);

 

 

 

'Developer' 카테고리의 다른 글

[ console ] commands  (0) 2012.10.08
maven + spring framework 연동하기  (0) 2012.10.04
Heuristic  (0) 2012.09.25
[ redis ] master , slave 설정  (0) 2012.09.14
[ memcached ] installation guide  (0) 2012.09.10
댓글