#!/bin/bash # Author: v.stone@163.com IP_LIST="" ROOT_PASSWORD="" SSH_ROOT="/root/.ssh" SCRIPTS_VERSION="cluster-exec v1.0" function help_doc { cat < Options: --ips IP List. e.g. 192.168.1.100-103 192.168.1.100 192.168.1.101 192.168.1.102 192.168.1.103 --password Root Password --help Print Help Example: $0 --ips "192.168.1.100-103" --password "password" $0 --help EOF exit 1 } function log_note { echo -e "\033[34;6m$@ \033[0m" return 0 } function log_succeed { echo -e "\033[32;6m[ SUCCEED ] $@ \n \033[0m" return 0 } function log_error { echo -e "\033[31;6m[ ERROR ] $@ \033[0m" exit 1 } function check_env { [[ "$(uname)" == "Linux" ]] || log_error "Only support Linux" [[ -z "${IP_LIST}" ]] && help_doc [[ -z "${ROOT_PASSWORD}" ]] && help_doc log_note "Start to check env" echo "${IP_LIST}" | grep -q '-' && { IP_PREFIX=$(echo "${IP_LIST}" | awk -F '.' '{print $1"."$2"."$3}') IP_START=$(echo "${IP_LIST}" | awk -F '.' '{print $4}' | awk -F '-' '{print $1}') IP_END=$(echo "${IP_LIST}" | awk -F '.' '{print $4}' | awk -F '-' '{print $2}') IP_LIST="" for ip_end in `seq ${IP_START} ${IP_END}` do IP_LIST="${IP_LIST} ${IP_PREFIX}.${ip_end}" done } for ip in ${IP_LIST} do ping -c 1 ${ip} || log_error "$ip cannot be ping" done which yum && PKG_INSTALL="yum install -y " which apt && PKG_INSTALL="apt install -y " for _cmd in ssh expect ssh-keygen ssh-copy-id do which ${_cmd} || ${PKG_INSTALL} ${_cmd} which ${_cmd} || log_error "$_cmd not found" done log_succeed "Checking complete" return 0 } function generate_ssh_key { log_note "Generate id_rsa and id_rsa.pub" [[ -f "${SSH_ROOT}/id_rsa" ]] && [[ -f "${SSH_ROOT}/id_rsa.pub" ]] && { log_succeed "ssh key is already in this machine" return 0 } ssh-keygen -t rsa -N '' -f ${SSH_ROOT}/id_rsa || log_error "Generation failure" log_succeed "Generation complete" return 0 } function _copy_ssh_id { _ip=${1?} _password=${2?} expect <> /etc/hosts" || log_error "ssh execution failure" done log_succeed "Complete to update all /etc/hosts files" return 0 } function generate_cluster_exec { log_note "Generate temp scripts of cluster-exec" temp_scripts='/tmp/cluster-exec' cat < ${temp_scripts} #!/bin/bash node=\${1?} cmd=\${2?} master=\$(grep '# cluster-exec master' /etc/hosts | awk '{print \$1}') slaves=\$(grep '# cluster-exec slave' /etc/hosts | awk '{print \$1}') exec_nodes="" if [[ "\${node}" == "all" ]]; then exec_nodes="\$master \$slaves" elif [[ "\${node}" == "master" ]]; then exec_nodes="\$master" elif [[ "\${node}" == "slave" ]]; then exec_nodes="\$slaves" fi for host in \${exec_nodes} do echo -e "\nExecuted Command In \${host}:" ssh root@\${host} "\$cmd" done echo -e "\n" EOF chmod +x ${temp_scripts} log_succeed "cluster-exec scripts generation complete" log_note "Copy cluster-exec into all nodes" for ip in ${IP_LIST} do log_note "scp to ${ip}" scp ${temp_scripts} root@${ip}:/usr/local/bin/ || log_error "scp failure" done log_succeed "Complete to copy cluster-exec" return 0 } function _ssh_try { _host=${1?} expect <