您的位置:首页 > 运维架构 > Shell

Linux下一个shell脚本中的命令在多个服务器上执行

2017-12-07 00:00 627 查看
描叙:在一台服务器上执行一个shell脚本,这个shell中的命令要在多个服务器执行。

#!/bin/bash
if [ "$#" -ne 2 ] ; then
echo "USAGE: $0 -f server_list_file cmd"
exit -1
fi

file_name=$1
cmd_str=$2

cwd=$(pwd)
cd $cwd
serverlist_file="$cwd/$file_name"

if [ ! -e $serverlist_file ] ; then
echo 'server.list not exist';
exit 0
fi

while read line
do
#echo $line
if [ -n "$line" ] ; then
echo "DOING--->>>>>" $line "<<<<<<<"
ssh $line $cmd_str < /dev/null > /dev/null
if [ $? -eq 0 ] ; then
echo "$cmd_str done!"
else
echo "error: " $?
fi
fi
done < $serverlist_file

使用帮助:

执行:

./all.sh host_file_list 'rm -rf /lctdir/test.txt'

host_file_list中为服务器的地址,一行一个,如下:

192.168.0.100
192.168.0.101

192.168.0.102

'rm -rf /lctdir/test.txt' 为要执行命令,这个命令执行完的效果,就是把三台服务器的/lctdir/test.txt删除了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: