用于检查远程服务器进程的 Perl 脚本 [英] Perl script to check remote server for process

查看:40
本文介绍了用于检查远程服务器进程的 Perl 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置的脚本有点问题.一点背景:

I am having a bit of a problem with a script that I setup. A little background:

该脚本的作用是从以 :: 分隔的文本文件中的服务器列表中读取服务器列表,登录服务器,查看 mysql 是否正在运行并返回报告.该文件配置为每行具有: Servername::Ip address::port number

The function of the script is to read from a list of servers that is in a text file separated by :: , log on to the servers, check to see that mysql is running and report back. The file is configured such that each line has: Servername::Ip address::port number

我遇到的问题是,我认为 perl 试图将我提供给代码中的函数的 IP 地址连接起来.谁能指出我正确的方向?

The problem I am having is that I think perl is trying to concatenate the ip address that I am feeding to the function I have in the code. Can anyone point my in the right direction?

#!/usr/bin/perl                                                                                           

use strict;
use warnings;

open(FH, '<', 'serverlist_test') or error("Cannot open file , ($!)");
while (my $line = <FH>) {
    our ($name, $ip, $port) = split(/::/, $line);
    my $version = &MySQL_check($ip, $port);                                                                                    
}
close FH;

sub MySQL_check {

    my $issue = `ssh -t root@"$_[0]" -p$"_[1]" 'ps axco command | grep -i mysql'`;
    print $issue;
    if ($issue =~ /mysql/) {                                                                             
      return "Mysql found"; 
    } else {                                                                                             
       return "Mysql not found";                                                                         
    }                                                                                                    
}

我做错了什么?

谢谢.

推荐答案

放入一些打印调试代码,以便您可以看到正在运行的命令.所以改变:

Put in some print-debugging code so you can see the command being run. So change:

my $issue = `ssh -t root@"$_[0]" -p$"_[1]" 'ps axco command | grep -i mysql'`;

my $command = qq`ssh -t root@"$_[0]" -p$"_[1]" 'ps axco command | grep -i mysql'`;
warn "Going to run \"$command\""; # comment this out when your code works!
my $issue = `$command`;

这应该用命令来标记问题.这几乎可以肯定是因为您没有chomp从文件中读取的行,所以端口号后面实际上有\n.

This should flag up the problem with the command. It's almost certainly because you didn't chomp the lines you read from the file, so the port number actually has \n after it.

这篇关于用于检查远程服务器进程的 Perl 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆