在做免密前要先手动生成公钥:
ssh-keygen -t rsa
敲击三次回车即可
写一个shell脚本:
#!/usr/bin/bash
ip=192.168.1.30
pass_word=123456
expect <<EOF
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@$ip
expect {
"yes/no" { send "yes\n";exp_continue}
"password" { send "$pass_word\n"}
}
expect eof
EOF
expect命令可以获取到命令返回结果并且根据指定内容进行自动发送相应字符。
如果你有多个主机可写一个txt文本 比如:1.txt
192.168.1.10 123456
192.168.1.20 123456
192.168.1.30 123456
脚本就可写为:
#!/usr/bin/bash
software=`rpm -q expect`
if [ $? -eq 0 ];then
echo "expect exist"
else
yum -y install expect
fi
while read line
do
ip=`echo $line |awk '{print $1}'`
pass=`echo $line |awk '{print $2}'`
expect <<EOF
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@$ip
expect {
"yes/no" { send "yes\n";exp_continue}
"password" { send "$pass\n"}
}
expect eof
EOF
done < /root/1.txt
评论区