ssh免密登陆脚本
运行脚本前面需要先配置好/etc/hosts文件的ip映射,这个脚本是根据hosts文件的ip进行免密登陆配置的
密码可以自己修改PWD_1
运行需要联网需要联网
#!/bin/bash
#yum安装expect
yum -y install expect
#PWD_1是登陆密码,可以自己设定
PWD_1=123456
ips=$(cat /etc/hosts |grep -v "::" | grep -v "127.0.0.1")
key_generate() {
expect -c "set timeout -1;
spawn ssh-keygen -t rsa;
expect {
{Enter file in which to save the key*} {send -- \r;exp_continue}
{Enter passphrase*} {send -- \r;exp_continue}
{Enter same passphrase again:} {send -- \r;exp_continue}
{Overwrite (y/n)*} {send -- n\r;exp_continue}
eof {exit 0;}
};"
}
auto_ssh_copy_id () {
expect -c "set timeout -1;
spawn ssh-copy-id -i $HOME/.ssh/id_rsa.pub root@$1;
expect {
{Are you sure you want to continue connecting *} {send -- yes\r;exp_continue;}
{*password:} {send -- $2\r;exp_continue;}
eof {exit 0;}
};"
}
rm -rf ~/.ssh
key_generate
for ip in $ips
do
auto_ssh_copy_id $ip $PWD_1
done
本脚本还有不完善的地方就是免密登陆只是单向进行,不能够互相的的免密登陆,哪个大神能够写个更完整的请贴到下面评论区,谢谢