ssh免密登录
#!/bin/bash
rpm -q expect
if [ $? -eq 0 ]; then
echo "已安装"
else
yum -y install expect
if [ $? -eq 0 ]; then
echo "安装成功"
else
echo "安装失败"
fi
fi
#本机是否有ssh密钥 没有则生成
if [ ! -f ~/.ssh/id_rsa ]; then
echo "请按3次enter健"
ssh-keygen -t rsa
fi
ssh_expect() {
expect -c "set timeout -1;
spawn ssh-copy-id root@$1
expect {
"yes/no" { send -- yes\r;exp_continue;}
"password:" { send -- $2\r;exp_continue;}
eof
}";
}
[ -f hosts.txt ] && rm -rf hosts.txt
#定义 hosts.txt
cat > hosts.txt << EOF
10.0.0.42
10.0.0.43
EOF
passwd=123456
for ip in `cat hosts.txt|awk '{print $1}'`
do
ssh_expect ${ip} ${passwd}
done
分发到需要做免密的服务器上,在添加所需服务器IP到hosts中
浙公网安备 33010602011771号