什么是系统命令返回?

时间:2014-03-20 12:15:40

标签: linux perl ssh system

我有一个像这样的模块。首先,我想解释它刚刚创建的所有logger($arg1,$arg2)看台输出。并且从.csv文件中读取参数$ source_server,@ source_user,@ log_name_pattern,@ log_name_pattern,@ archive_interval,@ move_interval,它们没有问题。

use constant OUTPUT_PATH => "./outputs/";
use constant MAX_COMMAND_LENGTH => 100000;
my $cmd_bash = "bash";
my $cmd_source = "source .profile";
sub run_remote_command_with_login
{
    my $start_time = time;
    my $host = shift;
    my $cmd = shift;
    my $user = shift;
    my $pwd = shift;
    my $is_import = 1 if (shift);

    if(length($cmd) > MAX_COMMAND_LENGTH)
    {
        logger(ERROR, "The command will not be run as it is longer than ".MAX_COMMAND_LENGTH." characters");
        return 0;
    }


    logger(DEBUG, "Opening telnet for ".$host." with user $user");

    $t = new Net::Telnet (Timeout => TIMEOUT, Errmode => ERRORMODE, Prompt => '/([>\$%~]|Logging out)\s*$/');
    $t->open($host);

    unless($t->login($user, $pwd)){
        logger(WARNING, "Login failed with return value $response");
        logger(WARNING, "Login failed to $host with user $user");
        return "";
    }
    $t->max_buffer_length(BUFFER);

    my @lines = $t->cmd($cmd_bash);
    @lines = $t->cmd($cmd_source);

    logger(DEBUG, "Running command: ".$cmd);

    if ($is_import)
    {
        $t->print($cmd);
        ($prematch, $match) = $t->waitfor('/.*Logging out.*/i');
        @lines = split(/\n/, $prematch);
        $ok = $t->close;
    }
    else
    {
        @lines = $t->cmd($cmd);
        $ok = $t->close;
    }   

    if($ok)
    {
        logger(DEBUG, "Closed telnet for ".$host);
    }
    else
    {
        logger(WARNING, "Could not close telnet for ".$host);
        logger(WARNING, "Error code: ".$ok);
    }

    logger(DEBUG, "Output lines are:"); 
    for($i = 0; $i < scalar(@lines); $i++)
    {
        logger(DEBUG, $lines[$i]);
    }
    logger(DEBUG, "End of output lines.");

    if (not -d OUTPUT_PATH)
    {
        my $cmd = "mkdir ".OUTPUT_PATH;
        `$cmd`;
    }
    my $out = OUTPUT_PATH.$start_time.".txt";
    open(DAT2, ">$out") || die("Could not open file!");
    logger(INFO, "Remote command execution finished in ".(time - $start_time)." seconds. (Output: $out)");

    for (my $i = 0; $i < scalar(@lines); $i++)
    {
        print DAT2 $lines[$i]."\n";
    }
    close(DAT2);

    return \@lines;
}

此模块是为远程服务器的Telnet连接编写的,它工作正常。但我们有一些Linux服务器,他们希望我通过SSH连接这些服务器。这不是问题,但在那个远程主机我们想要存档或移动文件。我们在Archive.pl中有这样的模块:

sub archive
{
    my $cmd_list = "find $source_dir -name \"$log_name_pattern\" -a ! -name \"*.gz\" -a ! -mtime -$archive_interval -a ! -size 0 -a -type f";
    my $cmd_gzip = "find $source_dir -name \"$log_name_pattern\" -a ! -name \"*.gz\" -a ! -mtime -$archive_interval -a ! -size 0 -a -type f -exec gzip {} \\;";

    Utilities::logger(Utilities::INFO,"Running command: $cmd_list");

    my $output = Utilities::run_remote_command_with_login($source_server, $cmd_list, $source_user, get_pwd($source_user));

    extract_files(\@$output);

    if(scalar(@$output) == 0)
    {
        Utilities::logger(Utilities::INFO,"No files found to archive.");
    }
    else
    {
        Utilities::logger(Utilities::INFO, scalar(@$output)." file(s) will be zipped:");
        for(my $i = 0; $i < scalar(@$output); $i++)
        {
            print $$output[$i];
        }   

        Utilities::logger(Utilities::INFO,"Running command: $cmd_gzip");
        if($source_user == "picshot"){
            $output = Utilities::run_remote_command_with_login($source_server, $cmd_gzip, $source_user, get_pwd($source_user));
        }
        else{
            $output = Utilities::run_remote_command_with_login($source_server, $cmd_gzip, $source_user, get_pwd($source_user));
        }
    }
}

sub move
{   
    my $cmd_filelist = "find $source_dir -name \"$log_name_pattern.gz\" -a ! -mtime -$move_interval -a -type f";
    Utilities::logger(Utilities::INFO,"Running command: $cmd_filelist");
    my $output = Utilities::run_remote_command_with_login($source_server, $cmd_filelist, $source_user, get_pwd($source_user));

    extract_files(\@$output);

    if(scalar(@$output) == 0)
    {
        Utilities::logger(Utilities::INFO,"No files found to move");
    }
    else
    {
        Utilities::logger(Utilities::INFO, scalar(@$output)." file(s) will be moved:");
        for(my $i = 0; $i < scalar(@$output); $i++)
        {
            print $$output[$i];
        }   

        if($source_server ne $target_server)
        {
            my $to_delete = Utilities::run_remote_ftp($source_server, $source_dir, $source_user, $source_user, $target_server, $target_dir, $target_user, $target_user, $output);

            if(scalar(@$to_delete) == 0)
            {
                Utilities::logger(Utilities::WARNING,"FTP put returned no files, keeping source files");
            }
            else
            {
                Utilities::logger(Utilities::INFO,"FTP put finished. Removing files returned by PUT");
                my $cmd_delete = "rm ";

                for(my $i = 0; $i < scalar(@$to_delete); $i++)
                {
                    $$to_delete[$i] = Utilities::trim($$to_delete[$i]);
                    $cmd_delete = $cmd_delete.$$to_delete[$i]." ";
                }
                Utilities::logger(Utilities::INFO,"Running command: $cmd_delete");
                Utilities::run_remote_command_with_login($source_server, $cmd_delete, $source_user, get_pwd($source_user));
            }
        }
        else
        {
            my $cmd_move = "mv ";
            for(my $i = 0; $i < scalar(@$output); $i++)
            {
                $$output[$i] = Utilities::trim($$output[$i]);
                $cmd_move = $cmd_move." ".$$output[$i];
            }
            $cmd_move = $cmd_move." ".$target_dir;

            # Create target dir if it does not exist.
            my $cmd_create_dir = "mkdir -p ".$target_dir;
            Utilities::logger(Utilities::INFO,"Running command: $cmd_create_dir");
            my $output = Utilities::run_remote_command_with_login($target_server, $cmd_create_dir, $target_user, get_pwd($target_user));

            Utilities::logger(Utilities::INFO,"Running command to move files");
            Utilities::logger(Utilities::INFO,"Running command: $cmd_move");
            $output = Utilities::run_remote_command_with_login($target_server, $cmd_move, $target_user, get_pwd($target_user));
            Utilities::check_move_cmd($output);
        }
    }
}

这两个模块工作正常。它们成功地归档和移动文件。

我在我要连接的服务器之间进行了ssh-keygen rsa配置。我试图在perl脚本上运行它:

my $cmd_gzip = "$source_dir -name \"$log_name_pattern\" -a ! -name \"*.gz\" -a ! -mtime -$archive_interval -a ! -size 0 -a -type f -exec gzip {} \\;";

system("ssh", "$source_user\@$source_server" , "find" , $cmd_gzip);

它成功连接到远程服务器,列出文件。但是,当我想要压缩我想要存档的文件时,它不起作用。控制台说“权限被拒绝”

位于顶部的归档模块具有extract_files模块。就像这样:

sub extract_files
{
    Utilities::logger(Utilities::DEBUG,"Entering sub: extract_files");
    my $temp = shift;

    my $size = scalar(@$temp);
    Utilities::logger(Utilities::DEBUG,"Initial array size is $size.");
    my $temp_log_name_pattern = $log_name_pattern;
    $temp_log_name_pattern =~ s/\*/.*/g;

    my $i = 0;
    while($i < $size)
    {
        Utilities::logger(Utilities::DEBUG,"Currently processing: $$temp[$i]. i = $i, size = $size");
        # Extract only files from output. " character in second part is used to filter out initial find command that can wrap and exist in the output.
        if ($$temp[$i] =~ $temp_log_name_pattern and $$temp[$i] !~ m/.*".*/)
        {
            Utilities::logger(Utilities::DEBUG,"Matched filename: $$temp[$i]");
            $i++;
        }
        else
        {
            Utilities::logger(Utilities::DEBUG,"Did not match filename: $$temp[$i]");
            splice(@$temp,$i,1);
            $size--;
        }
    }
    Utilities::logger(Utilities::DEBUG,"Exiting sub: extract_files");
}

我认为这会使脚本工作并避免错误“权限被拒绝”。但它得到一个这样的参数:

my $output = Utilities::run_remote_command_with_login($source_server, $cmd_list, $source_user, get_pwd($source_user));

    extract_files(\@$output);

所以现在我要问两件事,

  1. 我没有得到这个run_remote_command_with_login模块返回的内容,所以我无法创建我的ssh连接模块以正确使用所有模块。如何从系统命令返回?像run_remote_command_with_login模块获得system("ssh", "$source_user\@$source_server" , "find" , $cmd_gzip);(我没有写那个模块)

  2. 如果我错了,extract_files()模块无法避免“Permission denied”错误。我该怎么做 ?有没有想法。

  3. 我希望我能说出真正想要的一切。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您需要验证ZIPping的用户帐户是否具有正确的权限。您可能需要打印出用户帐户,在Linux下可以使用id命令完成。