#! /usr/local/bin/perl -s # ============================================================ # ftp-put # ============================================================ # ls -1 の出力結果のような書式のファイル名のリストを # 受け取り、そのファイルを $site に $user,$pass で送りつける # # Usage: # ls -1 | $0 -user=chigen -pass=1111 -site=www.lolita.com # ============================================================ $debug = 'on'; use Net::FTP; # CPAN にある Net::FTP を使う # Very thanks to tbs-i@cpsy.is.tohoku.ac.jp :D #### それぞれのオプションが省略されたときの対処 #### if($site !~ /./) { $site = '130.34.205.289'; } if($user !~ /./) { $user = 'shasinbu'; } if($pass !~ /./) { $pass = 'moemoe'; } if($RDIR !~ /./) { $RDIR = '.'; } @files = &get_filelist; &send_remote(@files); exit; # # sub get_filelist { while(<>) { s/[\*\/\@]*[ \t\r\n]*$//; s/^\.(\/)?//; next if (/\~$/ || /\/\./); if ( -f $_) { push(@ret,$_); } } return @ret; } # # remote_host にファイルを送る # sub send_remote { local(@files) = @_; if(defined($debug)) { $ftp = new Net::FTP($site, Debug => 1); } else { $ftp = Net::FTP->new($site); } # ftp でログイン if(defined($ftp) && $ftp->login($user,$pass)) { # set transfer mode to binary $ftp->binary(); # change the directory on the ftp site $ftp->cwd($RDIR); $mkd_done{'.'} = 'done'; &send_files(@files); $ftp->quit; } else { die "FTP failed: $!\n"; } } # # remote_host に個々のファイルを送る # sub send_files { local(@files) = @_; local($file, $path, $fname); foreach $file ( @files ) { local(@ps) = split(/\/+/,$file); print "file: $file .."; if( -d $file ) { if($mkd_done{$file} !~ /./) { $ftp->mkdir($file); $mkd_done{$file} = 'done'; } } elsif( -f $file ) { pop(@ps); local($p, $pt); foreach $p ( @ps ) { if($pt =~ /./) { $pt .= '/'; } $pt .= $p; if($mkd_done{$pt} !~ /./) { $ftp->mkdir($pt); $mkd_done{$pt} = 'done'; } } $ftp->put($file,$file); sleep 2; } else { print "[Error] Cannot find file: $file\n"; } print "done.\n"; } }