Perl 脚本检查是否可以 ssh 到远程服务器

Perl 脚本检查是否可以 ssh 到远程服务器

我有一个 perl 脚本,在其中我想在实际执行 ssh 之前首先检查是否可以 ssh 进入服务器。因此,如果可以 ssh,则继续 ssh 进入该服务器,否则适当处理此异常。

所以我有这个脚本,它遍历一个包含数据库列表的文本文件,并尝试 ssh 进入每个服务器以执行 bash 命令来检查服务器是否正在执行 MySQL SST:

    #!/usr/bin/perl -w

use strict;
use warnings;

use 5.010;
use DBI;
use Encode;
use IO::File;
use JSON;
use utf8;

BEGIN {
    binmode STDERR, ':utf8';
    binmode STDIN, ':utf8';
    binmode STDOUT, ':utf8';
}

my $time = localtime();
my $file = '/opt/db-servers/db.txt';
open my $info, $file or die "Could not open $file: $!";

while( my $hostname = <$info>)  {
    chomp( $hostname );
    my $xtrabk_check = `ssh $hostname ps -ef |grep mysql | grep wsrep_sst_xtrabackup-v2`;
    my $role_check = `ssh $hostname ps -ef |grep mysql | grep donor`;
    my $error_log = `ssh $hostname ps -ef |grep mysql`;
        if ( $xtrabk_check ne "" ){
            if ( $role_check ne "" ){
                my $cmd ="curl -vs -o /dev/null -X POST --data-urlencode 'payload={\"channel\": \"#db-wsrep-status-log\", \"username\": \"db-wsrep-status-log\", \"text\": \"$time: $hostname: --role Donor \n```$error_log```\", \"icon_emoji\": \":scorpion:\"}' 2>&1 /dev/null https://hooks.slack.com/services/GVadasdd/B6LSMF5GV/BIApnzoIldfdsrw343wf";
                system($cmd);
            }
            else {
                my $cmd ="curl -vs -o /dev/null -X POST --data-urlencode 'payload={\"channel\": \"#db-wsrep-status-log\", \"username\": \"db-wsrep-status-log\", \"text\": \"$time: $hostname: State transfer in progress, setting sleep higher mysqld \n```$error_log```\", \"icon_emoji\": \":scorpion:\"}' 2>&1 https://hooks.slack.com/services/GVadasdd/B6LSMF5GV/BIApnzoIldfdsrw343wf";
                system($cmd);
            }
       }
}
close $info;

因此,可能在执行下面的三行之前,我希望能够知道该服务器是否已关闭(因此无法 ssh 进入它)(然后使用以下命令 ssh 进入它):

my $xtrabk_check = `ssh $hostname ps -ef |grep mysql | grep wsrep_sst_xtrabackup-v2`;
    my $role_check = `ssh $hostname ps -ef |grep mysql | grep donor`;
    my $error_log = `ssh $hostname ps -ef |grep mysql`;

我怎样才能实现这一目标?顺便说一句,slack URL 已被更改,因此存在安全问题。

我在下面添加了建议的代码,但似乎仍然不起作用:

    ...
    while( my $hostname = <$info>)  {
    chomp( $hostname );
    my $ssh_ping = `$ssh_cmd\'@\'$hostname true`;
    if ( `$ssh_ping true`){
    my $xtrabk_check = `ssh $hostname ps -ef |grep mysql | grep wsrep_sst_xtrabackup-v2`;
    my $role_check = `ssh $hostname ps -ef |grep mysql | grep donor`;
    my $error_log = `ssh $hostname ps -ef |grep mysql`;
        if ( $xtrabk_check ne "" ){
            if ( $role_check ne "" ){
                my $cmd ="curl -vs -o /dev/null -X POST --data-urlencode 'payload={\"channel\": \"#db-wsrep-status-log\", \"username\": \"db-wsrep-status-log\", \"text\": \"$time: $hostname: --role Donor \n```$error_log```\", \"icon_emoji\": \":scorpion:\"}' 2>&1 /dev/null https://hooks.slack.com/services/GVadasdd/B6LSMF5GV/BIApnzoIldfdsrw343wf";
                system($cmd);
            }
            else {
                my $cmd ="curl -vs -o /dev/null -X POST --data-urlencode 'payload={\"channel\": \"#db-wsrep-status-log\", \"username\": \"db-wsrep-status-log\", \"text\": \"$time: $hostname: State transfer in progress, setting sleep higher mysqld \n```$error_log```\", \"icon_emoji\": \":scorpion:\"}' 2>&1 https://hooks.slack.com/services/GVadasdd/B6LSMF5GV/BIApnzoIldfdsrw343wf";
                system($cmd);
            } 
       }
    } #end $ssh_ping if
} #end while

答案1

你可以这样做:

`ssh $hostname true`;

if ($? == 0) {
    ...;
} else {
    die "ssh failed";
}

相关内容