在工作中大家肯定遇到过要进入不同的服务器执行命令,这个脚本实现了进入不同服务器执行相同命令,节省了大量的时间。
脚本如下所示:
#!/bin/bash
FILE_LIST=/opt/myshell/list
CMD=$1
NAME=$2
for IP in `cat $FILE_LIST|grep -v '^#'|grep "|$NAME" |awk -F'|' '{print $1}'`
do
echo "--------------------$IP------------------ "
ssh $IP "source /etc/profile; $CMD"
done
执行结果如下:
[root@hadoop3 myshell]# ./runRemoteCmd.sh "ls -lrt" shuguang
--------------------172.20.10.3------------------
total 12
-rw------- 1 root root 2026 8月 10 05:40 original-ks.cfg
-rw-r--r-- 1 root root 435 8月 10 05:40 anaconda-post.log
-rw------- 1 root root 2366 8月 10 05:40 anaconda-ks.cfg
--------------------172.20.10.4------------------
total 12
-rw------- 1 root root 2026 8月 10 05:40 original-ks.cfg
-rw-r--r-- 1 root root 435 8月 10 05:40 anaconda-post.log
-rw------- 1 root root 2366 8月 10 05:40 anaconda-ks.cfg
--------------------172.20.10.5------------------
total 12
-rw------- 1 root root 2026 8月 10 05:40 original-ks.cfg
-rw-r--r-- 1 root root 435 8月 10 05:40 anaconda-post.log
-rw------- 1 root root 2366 8月 10 05:40 anaconda-ks.cfg