1.主要脚本
vi mianmi.sh
#!/bin/bash
#------------------------------------------#
# FileName: 自动批量免密登陆
#maker=zsl
echo "" >/root/b.txt
rpm -q expect &>/dev/null
if [ $? -ne 0 ];then
yum -y install expect
fi
if [ ! -f /root/.ssh/id_rsa.pub ];then
ssh-keygen -P "" -f /root/.ssh/id_rsa &>/dev/null # 密钥对不存在则创建密钥
fi
while read line;do
ip=`echo $line | cut -d " " -f1` # 提取文件中的ip
user_name=`echo $line | cut -d " " -f2` # 提取文件中的用户名
pass_word=`echo $line | cut -d " " -f3` # 提取文件中的密码
port=`echo $line | cut -d " " -f4` #提取端口
ping -c1 -Wl $ip &>/dev/null
if [ $? -eq 0 ];then
{ echo "ssh -p $port $user_name@$ip">>/root/b.txt
expect <<EOF
set timeout 4
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub -p $port $user_name@$ip
expect {
"yes/no" { send "yes
";exp_continue}
"password" { send "$pass_word
"}
}
expect eof
EOF
}
fi
continue
done < /opt/ip.txt
vi /opt/ip.txt
192.168.1.62 root 123456 22 192.168.1.64 root 123456 23
如果卡住 ctrl +c 取消多推几次