一键SSH免密脚本
大型项目一键SSH免密脚本
#!/usr/bin/env bashroot=$( cd $(dirname $0)/.. pwd )source ${root}/dragonrcread -p "请您输入主机用户:" userif [ -z $user ];thenechoecho -e "\033[1;32;31m警告:您输入的用户名为空!\033[0m"echoexit 0firead -p "请您输入主机密码:" mimaif [ -z $mima ];thenechoecho -e "\033[1;32;31m警告:您第一次输入的密码为空!\033[0m"echoexit 0firead -p "请您再次输入密码:" querenif [ -z $queren ];thenechoecho -e "\033[1;32;31m警告:您第二次输入的密码为空!\033[0m"echoexit 0fiif [ $mima == $queren ];thenecho "您输入的密码为:$queren" && sleep 1echo "正在为您创建密钥对儿,请您注意" && sleep 1elseechoecho -e "\033[1;32;31m不好意思~您两次输入的密码不匹配,请您重新输入!\033[0m"echoread -p "请您输入主机用户:" userif [ -z $user ];thenechoecho -e "\033[1;32;31m警告:您输入的用户名为空!\033[0m"echoexit 0firead -p "请您输入主机密码:" mimaif [ -z $mima ];thenechoecho -e "\033[1;32;31m警告:您第一次输入的密码为空!\033[0m"echoexit 0firead -p "请您再次输入密码:" querenif [ -z $queren ];thenechoecho -e "\033[1;32;31m警告:您第二次输入的密码为空!\033[0m"echoexit 0fiif [ $mima == $queren ];thenecho "您输入的密码为:$queren"echo "正在为您创建密钥对儿,请您注意" && sleep 1elseechoecho -e "\033[1;32;31m抱歉~您两次输入的密码不匹配,请您重新执行脚本!\033[0m"echoexit 1fifi> /root/.ssh/authorized_keysif [ -f /root/.ssh/id_rsa.pub ];thenecho "您的主机已经有密钥对儿了,不需要再次创建"elsessh-keygen -t rsa -P "" -f ~/.ssh/id_rsafirpm -q sshpass &> /dev/nullif [ $? -ne 0 ];thenrpm -ivh ${root}/rpm/other/sshpass-1.06-2.el7.x86_64.rpm &> /dev/nullecho "StrictHostKeyChecking no" > /root/.ssh/configecho "UserKnownHostsFile=/dev/null" >> /root/.ssh/configelseecho "StrictHostKeyChecking no" > /root/.ssh/configecho "UserKnownHostsFile=/dev/null" >> /root/.ssh/configfiIFS=',' read -r -a all_nodes <<<"${ALL_NODES}"for ip in "${all_nodes[@]}"; dosshpass -p $queren ssh-copy-id $user@$ip &> /dev/nullssh $user@$ip "pwd" &> /dev/nullif [ $? -eq 0 ];thenecho "$ip is add successful"elseecho "$ip is failure"fidoneansible ${OTHER_NODES} -m shell -a "rm -rf /root/.ssh/id_rsa*"ansible ${OTHER_NODES} -m shell -a "ssh-keygen -t rsa -P \"\" -f ~/.ssh/id_rsa"echo -e "\033[5;32;31m温馨提示:主机双向互信ing...\033[0m"IFS=',' read -r -a nodes <<<"${OTHER_NODES}"for oip in "${nodes[@]}"; dossh -T $user@$oip cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keysdoneansible dragon -m copy -a "src=/root/.ssh/authorized_keys dest=/root/.ssh/authorized_keys"echo -e "\033[1;32;31m双向互信已完成!\033[0m"
赞 (0)