简体   繁体   English

如何在多个服务器上执行命令以执行命令

[英]How to execute command on multiple servers for executing a command

i have set of servers (150) for logging and a command (to get disk space).我有一组服务器(150 个)用于日志记录和一个命令(获取磁盘空间)。 How can i execute this command for each server.我如何为每个服务器执行此命令。 Suppose if script is taking 1 min to get report of command for single server, how can i send report for all the servers for every 10 min?假设如果脚本需要 1 分钟来获取单个服务器的命令报告,我如何每 10 分钟发送一次所有服务器的报告?

 use strict;
 use warnings;
 use Net::SSH::Perl;
 use Filesys::DiskSpace;


 # i have almost more than 100 servers..
 my %hosts   = (
    'localhost' => {
                       user  => "z",
                   password  => "qumquat",

    },


     '129.221.63.205' => {
                           user  => "z",
                       password  => "aardvark",

    },
'129.221.63.205' => {
                           user  => "z",
                       password  => "aardvark",

    },  


   );


  # file system /home or /dev/sda5 
  my $dir = "/home";

  my $cmd =  "df $dir";

  foreach my $host (keys %hosts) {
          my $ssh = Net::SSH::Perl->new($host,port => 22,debug => 1,protocol => 2,1 );    

      $ssh->login($hostdata{$host}{user},$hostdata{$host}{password} );
      my ($out) = $ssh->cmd($cmd});
          print "$out\n";
   }

It has to send output of disk space for each server它必须为每个服务器发送磁盘空间的输出

Is there a reason this needs to be done in Perl?是否有理由需要在 Perl 中完成? There is an existing tool, dsh , which provides precisely this functionality of using ssh to run a shell command on multiple hosts and report the output from each.有一个现有的工具dsh ,它提供了使用ssh在多个主机上运行 shell 命令并报告每个主机的输出的功能。 It also has the ability, with the -c (concurrent) switch to run the command at the same time on all hosts rather than waiting for each one to complete before going on to the next, which you would need if you want to monitor 150 machines every 10 minutes, but it takes 1 minute to check each host.它还具有使用-c (并发)开关在所有主机上同时运行命令的能力,而不是等待每个主机完成后再继续执行下一个,如果您想监视 150每 10 分钟检查一次机器,但检查每台主机需要 1 分钟。

To use dsh , first create a file in ~/.dsh/group/ containing a list of your servers.要使用dsh ,首先在~/.dsh/group/中创建一个包含服务器列表的文件。 I'll put mine in ~/.dsh/group/test-group with the content:我会将我的内容放在~/.dsh/group/test-group

galera-1
galera-2
galera-3

Then I can run the command然后我可以运行命令

dsh -g test-group -c 'df -h /'

And get back the result:并返回结果:

galera-3: Filesystem                 Size  Used Avail Use% Mounted on
galera-3: /dev/mapper/debian-system  140G   36G   99G  27% /
galera-1: Filesystem                 Size  Used Avail Use% Mounted on
galera-1: /dev/mapper/debian-system  140G   29G  106G  22% /
galera-2: Filesystem                 Size  Used Avail Use% Mounted on
galera-2: /dev/mapper/debian-system  140G   26G  109G  20% /

(They're out-of-order because I used -c , so the command was sent to all three servers at once and the results were printed in the order the responses were received. Without -c , they would appear in the same order the servers are listed in the group file, but then it would wait for each reponse before connecting to the next server.) (它们是乱序的,因为我使用了-c ,所以命令被一次发送到所有三个服务器,结果按照收到响应的顺序打印。没有-c ,它们将以相同的顺序出现服务器列在group文件中,但它会在连接到下一个服务器之前等待每个响应。)

But, really, with the talk of repeating this check every 10 minutes, it sounds like what you really want is a proper monitoring system such as Icinga (a high-performance fork of the better-known Nagios ), rather than just a way to run commands remotely on multiple machines (which is what dsh provides).但是,实际上,说到每 10 分钟重复一次检查,听起来您真正想要的是合适的监控系统,例如Icinga (著名Nagios的高性能分支),而不仅仅是一种在多台机器上远程运行命令(这是dsh提供的)。 Unfortunately, configuring an Icinga monitoring system is too involved for me to provide an example here, but I can tell you that monitoring disk space is one of the checks that are included and enabled by default when using it.不幸的是,配置 Icinga 监控系统太复杂了,我无法在此提供示例,但我可以告诉您,监控磁盘空间是使用时默认包含和启用的检查之一。

There is a ready-made tool called Ansible for exactly this purpose.有一个名为 Ansible 的现成工具就是为了这个目的。 There you can define your list of servers, group then and execute commands on all of them.在那里你可以定义你的服务器列表,然后对所有服务器进行分组和执行命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM