在Linux上的perl脚本中使用SCP上传失败

时间:2015-03-23 16:17:59

标签: linux perl scp

我在centos linux上安装了perl脚本,使用SCP每天午夜将特定目录的内容上传到远程服务器B.

添加了Cron作业并且工作正常。

现在它突然停止上传文件。

当脚本手动运行时,它运行并且shell恢复正常,但没有文件上传到服务器B.

当我检查日志文件条目 / var / log / messages 时,没有错误。

两台服务器之间的连接都很好。

此外,我在/ var / log / messages中收到此错误:

getaddrinfo: hk Name or service not known

这是与SCP问题有关还是其他问题?

代码段:

#!/usr/bin/perl -w
BEGIN {
    #sig handlers to clear lockfile in case of error
    $SIG{__DIE__} = \&sigdie;
    $SIG{INT}     = \&sigint;
    $SIG{TERM}    = \&sigterm;
    $SIG{ABRT}    = \&sigabrt;
}

        use Net::FTP;
        use Expect; #for scp

        my $local_dir   = $home.'stats/';
        my $last_xfer   = $home.'last_xfer/';
        my $last_upload;

        lg("Starting export.", 'log');
        if ($mode eq 'scp'){
             $last_upload = do_scp($server, $user, $pass, $files, $remote_dir);
        age_out_files($local_dir);
        my $run_time = time() - $time;
        lg("$0 successfully complete run in $run_time seconds.", 'log');

        sub do_scp {
            my $server    = shift;
            my $user      = shift;
            my $password  = shift;
            my $files     = shift;
            my $remotedir = shift;

            my $last_uploaded = 0;
            my $connect_error = 0;
            my $file_error; 

            my $copy_errors = 0;


            foreach my $file (keys %$files){
                my $last_time = $$files{$file};
                my $command = "scp $file $user\@$server:$remotedir";

                lg("get_scp running $command", 'debug');

                my $exp = Expect->spawn($command);

                $exp->log_stdout(0);
                #$exp->debug(3);

                $exp->expect(
                    $timeout,

                    [qr'assword:',
                        sub {
                            my $fh = shift;
                            $fh->send("$password\n");
                            $exp->expect($timeout,
                                [qr'Now try',
                                    sub {
                                            exp_continue;
                                    }
                                ],
                                [timeout =>
                                    sub {
                                        lg("do_scp: Failed to copy File : $file to server $server invalid password.", 'error');
                                        $connect_error = 1;
                                    }
                                ],
                            );
                        }
                    ],
                    [qr'Now try',
                        sub {
                            exp_continue;
                        }
                    ],
                    [qr'Are you sure',
                        sub {
                            my $fh = shift;
                            $fh->send("yes\n");
                            exp_continue;
                        }
                    ],
                    [timeout =>
                        sub {
                             lg("do_scp: Failed to copy File $file to server $server, scp timeout occurred.", 'error'); 
                             $file_error = 1;
                             $copy_errors++;                     
                        }
                    ],
                );

                return if $connect_error; 

                unless ($file_error){
                    if ($last_uploaded < $last_time){
                        $last_uploaded = $last_time; 
                        lg("updating last_uploaded to $last_time", 'debug');
                    }
                }

            }
            return $last_uploaded;
        }
    sub lg {
        my $message = shift;
        my $level   = shift;

        unless (defined $level){
            $level = 'debug';
        }
        if ($DEBUG){
            print scalar localtime()." $message\n";
        }       
        if ($level eq 'log'){
            logprint("local1.debug", "INF: ", "$0", $message);
        }
        elsif ($level eq 'error'){
            logprint("local1.debug", "ERR: ", "$0", $message);
        }



    }
sub err {
    my $message = shift;
    lg($message);
    die $message;
}

sub logprint {
    my ($pri, $type, $tag, $log) = @_;
    `logger -p $pri -i "$type" -t "$tag" "$log"`;
}

0 个答案:

没有答案