#!/bin/bash
# 请编辑
host_list=(10.20.10.171 10.20.10.172 10.20.10.173 10.20.10.174)
usage="该脚本用来进行分发命令或者脚本,使用前,请先编辑IP列表,保证所有IP均可以在脚本所在机器免密登录!\n用法: distribute.sh (command|file) args"
usage_command="分发命令模式,至少需要2个参数,第二个参数为执行的命令\nUsage: distribute.sh command 'exampleCommand'"
usage_file="分发文件的命令,至少需要3个参数,第二个参数为本地文件名称,第三个参数是目标目录\nUsage: distribute.sh file 'localFile' 'destinationDirectory'"
# 检查参数
args_length=$#
if [ $args_length -lt 1 ]
then
echo -e $usage
exit 1
fi
type=$1
if [ "$type" = "command" ];then
if [ $args_length -lt 2 ];then
echo -e $usage_command
exit 1
fi
elif [ "$type" = "file" ];then
if [ $args_length -lt 3 ];then
echo -e $usage_file
exit 1
fi
else
echo -e $usage
exit 1
fi
# 循环执行输入的命令
for ip in ${host_list[@]}
do
echo "开始在${ip}执行命令.."
if [ "$type" = "command" ];then
ssh $ip "$2"
else
scp -r -p $2 ${ip}:$3
fi
echo "${ip} 已完成"
done
分发脚本-文件或者命令
最新推荐文章于 2024-04-08 00:13:39 发布