我有一個Perl腳本,我想先檢查是否有可能ssh到服務器之前,我實際上做ssh。所以如果有可能ssh,然後繼續ssh進入該服務器,否則適當處理此異常。Perl腳本來檢查是否有可能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`;
如何我可以去實現這個嗎?順便說一句,鬆懈的網址已被改變,所以安全擔憂。