先安裝 expect bash
yum install expect服務器
#!/bin/bashssh
#ide
#列出要免密的服務器spa
SERVERS="192.168.1.1 192.168.1.2 192.168.1.3"it
#服務器密碼class
PASSWORD=123456配置
#模擬配置免密過程循環
auto_ssh_copy_id() {密碼
#設置不超時
expect -c "set timeout -1;
#執行ssh-copy-id命令
spawn ssh-copy-id $1;
#模擬輸入
expect {
*(yes/no)* {send -- yes\r;exp_continue;}
*assword:* {send -- $2\r;exp_continue;}
eof {exit 0;}
}";
}
#循環須要進行免密的服務器
ssh_copy_id_to_all() {
for SERVER in $SERVERS
do
auto_ssh_copy_id $SERVER $PASSWORD
done
}
ssh_copy_id_to_all