#!/bin/bash # #1.为脚本添加远程主机的用户及密码 ############################################################################# read -p "请您输入主机用户:" user read -p "请您输入主机密码:" mima read -p "请您再次输入密码:" queren if [ $mima == $queren ] then echo "您输入的密码为:$queren" && sleep 1 echo "正在为您创建密钥对儿,请您注意" && sleep 1 else echo "您两次输入的密码不匹配,请您重新输入" read -p "请您输入主机用户:" user read -p "请您输入主机密码:" mima read -p "请您再次输入密码:" queren if [ $mima == $queren ] then echo "您输入的密码为:$queren" echo "正在为您创建密钥对儿,请您注意" && sleep 1 else echo "您两次输入的密码不同,请您重新执行脚本" fi fi ############################################################################# #2.检查秘钥、创建密钥 if [ -f /root/.ssh/id_rsa.pub ] then echo "您的主机已经有密钥对儿了,不需要再次创建" else ssh-keygen -t rsa -P '' fi rpm -q sshpass &> /dev/null if [ $? -ne 0 ] then yum -y install sshpass &> /dev/null echo "StrictHostKeyChecking no" > /root/.ssh/config echo "UserKnownHostsFile=/dev/null" >> /root/.ssh/config else echo "StrictHostKeyChecking no" > /root/.ssh/config echo "UserKnownHostsFile=/dev/null" >> /root/.ssh/config fi #3.批量添加密钥对儿 ip=`cat ~/ip.txt` for i in $ip do sshpass -p $queren ssh-copy-id $user@$i &> /dev/null ssh $user@$i "pwd" &> /dev/null if [ $? -eq 0 ] then echo "$i is add successful" else echo "$i is failure" fi done
转载于:https://blog.51cto.com/wxtan/1947711