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删除了。
#!/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删除了。
相关文章推荐
- php 执行linux命令,shell脚本函数,获取服务器运行状态值
- linux下shell脚本执行方法及exec和source命令
- android 在手机sd 卡中执行linux 命令 和shell 脚本
- windows 下 java程序jsch连接远程linux服务器执行shell命令
- linux的shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量
- Linux中执行shell脚本命令的4种方法总结
- Linux中使用crontab命令定时执行shell脚本或其他Linux命令
- Linux执行shell脚本方式及区别&命令后台运行
- Linux学习笔记:bash特性之多命令执行,shell脚本
- Java 连接远程Linux 服务器执行 shell 脚本查看 CPU、内存、硬盘信息
- Linux执行shell脚本方式及区别&命令后台运行
- linux下shell脚本执行交互式操作命令 —【expect】
- Linux 定时执行shell脚本命令之crontab
- linux 通过shell脚本在本地执行远程机器命令
- linux下shell脚本执行方法及exec和source命令
- [Linux]定时执行命令的shell脚本,删除脚本自身,判断输出是否为空
- linux的shell脚本中执行多个命令的方法
- 简易登录,批量执行命令,批量copy文件shell脚本【Linux运维之道之脚本案例】
- Windows远程linux服务器执行shell命令
- linux中利用shell脚本条件执行命令