1. 程式人生 > >自動設定ssh免密登入指令碼

自動設定ssh免密登入指令碼

自動設定ssh免密登入指令碼

`#!/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

[email protected]$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
`