Skrip Perl untuk memeriksa server jarak jauh untuk proses (Perl script to check remote server for process)


問題描述

Skrip Perl untuk memeriksa server jarak jauh untuk proses (Perl script to check remote server for process)

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:

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

look at 

‑p$"_[1]"

which should be

‑p "$_[1]"

方法 2:

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
...

方法 3:

Try

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

方法 4:

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.

(by RoncioiuugexeRohitWamphibientpndc)

參考文件

  1. Perl script to check remote server for process (CC BY‑SA 3.0/4.0)

#perl #scripting #linux






相關問題

保持特定位數的簡單 Perl 數學 (simple Perl math while keeping a specific number of digits)

如何在 Windows 批處理腳本或 Perl 中將文件移動到回收站? (How can I move files to the Recycle Bin in a Windows batch script or Perl?)

從子程序返回數組 (Return array from subroutine)

我可以以與操作系統無關的方式限制 Perl 進程使用的內存嗎? (Can I Iimit the memory used by a Perl process in an OS-agnostic way?)

$# 在 perl 中接受什麼作為輸入? (what does $# accept as input in perl?)

Perl Text::CSV_XS 從字符串中讀取 (Perl Text::CSV_XS read from string)

使用 excel 2010 更新批處理文件中的變量 (use excel 2010 to update variables in batch file)

在 perl 中為哈希添加值 (Adding value to an hash in perl)

為什麼 perl 會忽略我的正則表達式中的多餘字符? (Why does perl ignore extra characters in my regex?)

boost::regex - \bb? (boost::regex - \bb?)

如果小於 X 天,如何從磁盤讀取文件,如果舊,則重新獲取 html 文件 (How to read a file from the disk if less than X days old, if older, refetch the html file)

使用 Devel-Cover 獲取覆蓋率報告 (Using Devel-Cover to get coverage reports)







留言討論