Perl script to check remote server for process

跟風遠走 提交于 2019-12-13 04:47:44

问题


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

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

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";                                                                         
    }                                                                                                    
}

What am I doing wrong?

Thank You.


回答1:


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'`;

to

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`;

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.




回答2:


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

look at

-p$"_[1]"

which should be

-p "$_[1]"



回答3:


Your code with a few modifications

...
while (my $line = <FH>) {
    chomp($line); #MOD -- remove newline
    our ($name, $ip, $port) = split("::", $line); #MOD -- change delimiter
...

sub MySQL_check {

    my $issue = `ssh -t root@"$_[0]" -p"$_[1]" 'ps axco command | grep -i mysql'`; #MOD -- fix misplaced double quotes
...



回答4:


Try

our ($name, $ip, $port) = split('::', $line);


来源:https://stackoverflow.com/questions/13278738/perl-script-to-check-remote-server-for-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!