perl ftp一问题

perl ftp一问题

大家好,请教一个问题
问题描述:现有一个perl写的FTP程序,我想实现一个功能,自动检查FTP是否成功,前两次失败的话就重新FTP,如果第三次还失败的话,就退出.并且把日志文件写到自定的文件中.
我的思路:通过读取日志文件,查找指定的字符串'successfully'来判断是否FTP成功,'can't'来判断FTP是否失败.
程序改好后执行是出现的问题pen 日志文件时出错.不知道是逻辑错误,还是那错了,请各位指教
程序执行过程:通过一个shell 调用perl,产生日志文件
以下是perl程序原码,主要是对FTP程序做了改动
#!/usr/local/bin/perl -w



use strict;

use warnings;



use DBI;

use Data:umper;

use Net::FTP;

my $filepath="./lbsdata";
print "unload begins.\n";

my $title = "#PROVINCE        CITY        SITE_ID        MICRO?        LAC CELL_ID BSIC    BCCH    X_deg   X_min   X_sec   Y_deg   Y_min   Y_sec   BORE    HEIGHT(m)   EIRP    BA_IND  NUM_BA  BA_1    BA_2    BA_3    BA_4    BA_5    BA_6    BA_7    BA_8    BA_9    BA_10   BA_11   BA_12   BA_13   BA_14   BA_15   BA_16   BA_17   BA_18   BA_19   BA_20   BA_21   BA_22   BA_23   BA_24   BA_25   BA_26   BA_27   BA_28   BA_29   BA_30   BA_31   BA_32   BA_IND  NUM_BA  BA_1    BA_2    BA_3    BA_4    BA_5    BA_6    BA_7    BA_8    BA_9    BA_10   BA_11   BA_12   BA_13   BA_14   BA_15   BA_16   BA_17   BA_18   BA_19   BA_20   BA_21   BA_22   BA_23   BA_24   BA_25   BA_26   BA_27   BA_28   BA_29   BA_30   BA_31   BA_32";

   $title =~ s/\s+/\t/g;



my $currentTime = &getTimestamp(1);

   $currentTime = substr $currentTime, 0, 8;

print "date: $currentTime\n";



my $filePath = '/usr/local/apache2/htdocs/irms/BOCO.IRMS/local/bin/lbsdata';
my $ftpfilePath = '/data/bsdata';
my $ftpfilePath2 = '/opt/edis/lbs';

print "file name: $filePath/sc$currentTime.txt\n";

open FILE, ">$filePath/sc$currentTime.txt" or die "open file failed: $!\n";

print FILE $title, "\n";



print "connect to database:\n";

my $dbh = &dbconnect();

my $sql = q/select 'sichuan',
b.toll_code,

c.site_id,--站号

'',
a.lac,

a.cell_id,

a.ncc||a.bcc, --为空填充50

a.bcch_freq,--为空填充50

a.longitude,
' ',         --用tab键填充

' ',

a.latitude,
' ',

' ',

a.ant_dirct_angle,--为空填充50 天线方位角

a.ant_height,     --为空填充50 天线高度

a.bts_trx_power,  --为空填充50 发射功率

a.Ba_ind_idel,    --相邻小区部分没有数据以tab键填充

a.Ba_num_idel,

a.Ba_idle1  ,

a.Ba_idle2  ,

a.Ba_idle3  ,

a.Ba_idle4  ,

a.Ba_idle5  ,

a.Ba_idle6  ,

a.Ba_idle7  ,

a.Ba_idle8  ,

a.Ba_idle9  ,

a.Ba_idle10 ,

a.Ba_idle11 ,

a.Ba_idle12 ,

a.Ba_idle13 ,

a.Ba_idle14 ,

a.Ba_idle15 ,

a.Ba_idle16 ,

a.Ba_idle17 ,

a.Ba_idle18 ,

a.Ba_idle19 ,

a.Ba_idle20 ,

a.Ba_idle21 ,

a.Ba_idle22 ,

a.Ba_idle23 ,

a.Ba_idle24 ,

a.Ba_idle25 ,

a.Ba_idle26 ,

a.Ba_idle27 ,

a.Ba_idle28 ,

a.Ba_idle29 ,

a.Ba_idle30 ,

a.Ba_idle31 ,

a.Ba_idle32 ,

a.Ba_ind_busy,

a.Ba_num_busy,

a.Ba_busy1  ,

a.Ba_busy2  ,

a.Ba_busy3  ,

a.Ba_busy4  ,

a.Ba_busy5  ,

a.Ba_busy6  ,

a.Ba_busy7  ,

a.Ba_busy8  ,

a.Ba_busy9  ,

a.Ba_busy10 ,

a.Ba_busy11 ,

a.Ba_busy12 ,

a.Ba_busy13 ,

a.Ba_busy14 ,

a.Ba_busy15 ,

a.Ba_busy16 ,

a.Ba_busy17 ,

a.Ba_busy18 ,

a.Ba_busy19 ,

a.Ba_busy20 ,

a.Ba_busy21 ,

a.Ba_busy22 ,

a.Ba_busy23 ,

a.Ba_busy24 ,

a.Ba_busy25 ,

a.Ba_busy26 ,

a.Ba_busy27 ,

a.Ba_busy28 ,

a.Ba_busy29 ,

a.Ba_busy30 ,

a.Ba_busy31 ,

a.Ba_busy32

from bts a ,region b ,btssitemanager c

where a.site_id = c.int_id

and a.region_id = b.int_id

and a.longitude is not null and a.latitude is not null

and a.confirmed=1 and c.site_id is not null and c.site_id <>''
order by a.region_id/;


print "sql: $sql\n";

# is_micro_cell

my $ADJ_CELL_POSITION        = 19; # position of Ba_idle1

my $ANT_DIRCT_ANGLE_POSITION = 14;

my $IS_MICRO_POSITION        = 3;



# default 50

my @DEFAUT_50_POSITION       = ( 6, 7, 14, 15, 16 );


# longitude, latitude
my $LONGITUDE_POSITION       = 8;
my $LATITUDE_POSITION        = 11;


my $sth = $dbh->prepare( $sql )

    or die "prepare failed: $sql\n";

   $sth->execute();

   

my @data;

my $line = 0;

while( @data = $sth->fetchrow_array() ) {

        $line++;

#        print "$line unload.\r";
        foreach( @data ) { # remove tail white spaces

                if( defined $_ ) {

                        s/\s+$//;

                }

        }

        @data = &convertData( @data );

        print FILE join( "\t", @data ), "\n";

       

#        if( $line == 7214 ) {
#                print Dumper \@data;
#                last;

#        }

#        exit;

}
print "total $line unloaded.\n";


print "unload finish.\n";

close FILE;

$sth->finish();

$dbh->disconnect();


print "transfer data via FTP...\n";
&ftpPut();
&ftpPut2();


exit;



sub dbconnect {

        my $dbh;

       

        my $dsn = 'dbi:Informix:edis2@edisdb1';

        my $username = 'informix';

        my $password = 'boco#informix';



        print "dsn: $dsn\n";

        eval { $dbh = DBI->connect( $dsn, $username, $password, { AutoCommit      => 1,

                                                               RaiseError      => 1,

                                                               odbc_cursortype => 2}  );

        };

       

        if ($@) {

                print " can't connect to DSN: $dsn.\n". "$DBI::errstr, $@\n";

                die " can't connect to DSN: $dsn.\n". "$DBI::errstr\n";

        }

       

        return $dbh;

}



sub isNull {

        if( defined $_[0] and $_[0] !~ /^\s*$/ ) {

                return 0;

        } else {

                return 1;

        }

}



sub convertData {

        my @data = @_;

       

        if( not @data ) {

                print "data(line) is null\n";

                exit;

        }

       

#        print Dumper \@data;

       

#        $data[0] = 'sichuan';

#        is_micro_cell

        if( &isNull($data[$ADJ_CELL_POSITION]) or &isNull($data[$ANT_DIRCT_ANGLE_POSITION]) ) {

#                print "$data[$ADJ_CELL_POSITION], $data[$ANT_DIRCT_ANGLE_POSITION]\n";
#                print "Y\n";
                $data[$IS_MICRO_POSITION] = 'Y';

        } else {

#                print "$data[$ADJ_CELL_POSITION], $data[$ANT_DIRCT_ANGLE_POSITION]\n";
#                print "N\n";
                $data[$IS_MICRO_POSITION] = 'N';

        }

#        exit;

        $data[14] =~ s/_$//;
#        if( $data[14] =~ /^-/ ) {
#                $data[14] = 0;
#        }
#        default 50

        foreach my $position ( @DEFAUT_50_POSITION ) {

                if( &isNull($data[$position]) ) {

                        $data[$position] = 50;

                }

        }

       


#        latitude, longitude
        foreach( ($LONGITUDE_POSITION, $LATITUDE_POSITION) ) {
                if( $data[$_] =~ /^\d+\.\d{6}/ ) {
                        $data[$_] = $&;
                }
        }
                       
#        0.00000E+00
        for( my $i = 0; $i < scalar @data; $i++ ) {

                if( $i > 17 and &isNull($data[$i]) ) {
                        $data[$i] = '33237_10242';
                } elsif( not &isNull($data[$i]) and $data[$i] =~ /^0\.00000E\+00$/ ) {
                        $data[$i] = 0;
                }
        }

       

#        print Dumper \@data;

       

        return @data;

}





sub getTimestamp () {

   my $format = shift;



   my ( $sec, $min, $hour, $day, $mon, $year ) = localtime( time );

   $mon += 1;

   $year += 1900;

   my $time;

   if( defined $format ) {

      if ( $format == 1 ) {

#   if ( defined $format ) {

        $time = sprintf "%04d%02d%02d%02d%02d%02d",$year,$mon,$day,$hour,$min,$sec;

      } else {

              $time = sprintf "%04d-%02d-%02d %02d:%02d:%02d",$year,$mon,$day,$hour,$min,$sec;

    }

   } else {

      $time = sprintf "%04d-%02d-%02d %02d:%02d:%02d",$year,$mon,$day,$hour,$min,$sec;

   }

   $time;

}





##################################################################
# put filesvia FTP
# Interface: ftpPut();
# Return: None
###################################################################  
sub ftpPut() {  
        my $count  = 0;#用来记录失败的次数
        my $line;       #用来按照行读取日志文件
        my $host   = '10.105.228.88';
        my $usr    = 'bsdata';
        my $passwd = 'bsdata';
        print "ftp begins;\n";
        my $ftp = Net::FTP->new( $host, Debug =>0 );
           $ftp->login( $usr, $passwd ) or die "connect failed: $!\n";
       
#        $filePath/sc$currentTime.txt;
           $ftp->put( "$filePath/sc$currentTime.txt", "$ftpfilePath/sc$currentTime.txt" )# save to task data directory
             or die "cannot put the file sc$currentTime.txt\n";
                 print "now read log by line...\n";
        open (MYFILE,"putlbs.log" || die " open myfile failured!\n";
        while ($line = <MYFILE> {
                if ($line =~ /cannot/) {
                        print "$line\n";
                        $count++;
                }else {exit;}
        }
        print "read finish...\n";
        print "$count\n";
        if ($count > 0 && $count < 4) {
                &ftpPut();
        &ftpPut2();

        }else{
                exit;
        }
        print "put sc$currentTime.txt successfully.\n";
       
        return;
}

sub ftpPut2() {  
        my $host   = '10.101.9.48';
        my $usr    = 'edis';
        my $passwd = 'boco#edis';
        print "ftp to 10.101.9.48 begins;\n";
        my $ftp = Net::FTP->new( $host, Debug =>0 );
           $ftp->login( $usr, $passwd ) or die "connect failed: $!\n";
       
#        $filePath/sc$currentTime.txt;
           $ftp->put( "$filePath/sc$currentTime.txt", "$ftpfilePath2/sc$currentTime.txt" )# save to task data directory
             or die "cannot put the file sc$currentTime.txt\n";
        print "put sc$currentTime.txt successfully.\n";
       
        return;
}
shell程序
perl /usr/local/apache2/htdocs/irms/BOCO.IRMS/local/bin/unload.pl >>& /usr/local/apache2/htdocs/irms/BOCO.IRMS/local/bin/putlbs.log
出错信息:
can't open file at line ...
嗯,很好,千万别把 at line 多少告诉我们,不然我们把你的问题给迅速解决了那就不好玩了。
代码里面表情不够丰富
没人理我