分发脚本-文件或者命令

#!/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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值