超级批量管理神器:使用PSSH和Shell脚本实现多台服务器并发管理,替代Ansible和SaltStack

------> 课程视频同步分享在今日头条B站

------> 课程设计代码开发部分的文档,会保存一份在我的github上面,方便大家阅读。

大家好,我是博哥爱运维。

通常运维工作中,我们对于批量并发地管理服务器以及云主机,会使用Ansible或SaltStack工具,但这节课博哥并不打算讲这些,而是带大家定制开发一个批量管理工具脚本。

工具脚本
#!/bin/bash
# __author__: bogeit

Help(){
    echo "Usage:"
    echo "bash $0 ssh   (1.1.1.1|ip.txt) off 'ip r'"
    echo "bash $0 rsync (1.1.1.1|ip.txt) off local_dir      remote_dir"
    echo "bash $0 hss   (1.1.1.1|ip.txt) off local_save_dir remote_dir"

    exit 0
}

if [ $# -eq 0 ];then
    Help
fi


control="${1}"
ip_file="${2}"
waitStatus="${3:-on}"
cmd_dir="${4}"
remote_dir="${5}"


wait_enter(){
    if [[ $waitStatus == "on" ]];then
        echo "  ======== wait a monment, you can use CTRL+C to stop this step or Enter to continue. ========"
        read -p "" Wait_a_moment
    else
        echo ">>>"
    fi
}



only_ip_add(){
    hosts_file_check
    [ -f ${hosts_file}_onlyip ] && > ${hosts_file}_onlyip
    for i in `seq 0 $(expr $(cat ${hosts_file} |jq -r '."instanceIP"|keys|length') - 1)`
    do
    {
        api_ip=`cat ${hosts_file} |jq -r ".instanceIP[$i]"`
        echo "${api_ip}" >> ${hosts_file}_onlyip
    }&
    done
    wait
}


pssh_check(){
  if [ ! -f /etc/redhat-release ] && cat /etc/issue|grep -iw Ubuntu &>/dev/null;then
    if ! which parallel-rsync &>/dev/null;then
      apt-get install -y pssh
    fi
    ssh_cmd="$(which parallel-ssh)"
    rsync_cmd="$(which parallel-rsync)"
    hss_cmd="$(which parallel-slurp)"
  else
    if ! which pssh &>/dev/null;then
      yum install -y pssh
    fi
    ssh_cmd="$(which pssh)"
    rsync_cmd="$(which prsync)"
    hss_cmd="$(which pslurp)"
  fi
}

pssh_func(){
  echo "ssh to host excute command"
  echo "Usage: $0 ssh   ip or ip_file {on|off} \"command\""
  echo -e "Example:  xxxctl ssh   1.1.1.1 off 'ip r'\n"
  wait_enter
  pssh_check
  [ -f ${ip_file} ] && ${ssh_cmd} -O ConnectTimeout=1 -O StrictHostKeyChecking=no -P -h ${ip_file} -l $(whoami) ${cmd_dir} || ${ssh_cmd} -O ConnectTimeout=1 -O StrictHostKeyChecking=no -P -H ${ip_file} -l $(whoami) ${cmd_dir}
}

prsync_func(){
  echo "rsync local files to remote host"
  echo "Usage: $0 rsync   ip or ip_file {on|off}   local_file or dir   remote_dir"
  echo -e "Example:  xxxctl rsync 1.1.1.1 off local_dir      remote_dir\n"
  wait_enter
  pssh_check
  [ -f ${ip_file} ] && ${rsync_cmd} -O ConnectTimeout=1 -O StrictHostKeyChecking=no -h ${ip_file} -l $(whoami) -a -r ${cmd_dir} ${remote_dir} || ${rsync_cmd} -O ConnectTimeout=1 -O StrictHostKeyChecking=no -H ${ip_file} -l $(whoami) -a -r ${cmd_dir} ${remote_dir}
}

pslurp_func(){
  echo "rsync remote host files to local"
  echo "Usage: $0 hss   ip or ip_file {on|off}   local_save_dir   remote_dir"
  echo -e "Example:  xxxctl hss   1.1.1.1 off local_save_dir remote_dir\n"
  wait_enter
  pssh_check
  [ ! -d ${cmd_dir} ] && mkdir -p ${cmd_dir}
  [ -f ${ip_file} ] && ${hss_cmd} -O ConnectTimeout=1 -O StrictHostKeyChecking=no -h ${ip_file} -l $(whoami) -r -L ${cmd_dir} ${remote_dir} $(basename ${remote_dir}) || ${hss_cmd} -O ConnectTimeout=1 -O StrictHostKeyChecking=no -H ${ip_file} -l $(whoami) -r -L ${cmd_dir} ${remote_dir} $(basename ${remote_dir})
}

main(){
    case $control in
        ssh)
        pssh_func
        ;;
        rsync)
        prsync_func
        ;;
        hss)
        pslurp_func
        ;;
        *)
        Help
    esac
}


main


ip列表配置示例

如果ssh端口都是默认的22,那么下面配置里面的:22可以省略

10.0.1.201:22
10.0.1.202:22
10.0.1.203:22
10.0.1.204:22
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值