通过 SSH 连接到多个服务器执行多个命令

通过 SSH 连接到多个服务器执行多个命令

我有几台主机需要安装一次性 RPM 以及属于 CentOS-Debuginfo.repo 的软件包。这些机器当前禁用了 repo 并启用了 gpgcheck。安装完所有内容后,我需要 rpm --nodeps -e 一些软件包,同时回显已完成所有操作的每台计算机的主机名。

到目前为止,我有这个:

#!/bin/bash
# SSH to hosts, then grab the GTS RPM and install it.  Yum install required debugging packages, then rpm to erase unnneeded ones.

cmds="echo $HOSTNAME; cd /tmp && wget http://download.sipxcom.org/pub/epel.bak/6/x86_64/gts-0.7.6-19.20111025.el6.x86_64.rpm /tmp; rpm -i /tmp/gts-0.7.6-19.20111025.el6.x86_64.rpm; yum --enablerepo debug --nogpg install -y db4-debuginfo.x86_64 expat-debuginfo.x86_64 glibc-debuginfo.x86_64 kernel-devel.x86_64 nss-softokn-debuginfo.x86_64 openssl-debuginfo.x86_64 python-debuginfo.x86_64 readline-debuginfo.x86_64 zlib-debuginfo.x86_64; for i in $(cat /tmp/testing/rpms_to_erase.txt); do rpm -e $i; done"
hostlist=/tmp/testing/hostlist.txt

for i in $HOSTLIST
  do
    ssh -qn $i "bash $CMDS" && "rpm -e gdm-plugin-fingerprint.x86_64 gdm-user-switch-applet.x86_64 gnome-applets.x86_64"
  done

如您所见,这非常丑陋。想知道是否有人有更优雅的解决方案。

答案1

Clusterssh(命令 cssh)可让您同时 ssh 到多个主机

cssh server1 server2 server3

然后无论您输入什么,一旦激活,它就会出现在所有终端中。

相关内容