配置免密登录
Tutorial: 运维相关
Category: Shell
Published: 2026-04-07 13:58:25
Views: 22
Likes: 0
Comments: 0
#!/usr/bin/expect
VMS=(
192.168.0.170
192.168.0.171
192.168.0.172
192.168.0.173
192.168.0.174
)
SERVER="192.168.0.175"
SERVER_PWD="123456"
CLIENT_PWD="654321"
function config_client() {
for vm in ${!VMS[@]}; do
sleep 1
expect <<EOF
spawn ssh ${VMS[$vm]}
expect "yes/no" {send "yes\r"}
expect "assword" {send "${CLIENT_PWD}\r"}
expect "#" {send "rm -rf ~/.ssh/*\r"}
expect "#" {send "ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa -q\r"}
expect "#" {send "ssh-copy-id ${SERVER}\r"}
expect "yes/no" {send "yes\r"}
expect "assword" {send "${SERVER_PWD}\r"}
expect "#" {send "exit\r"}
expect "#" {send "exit\r"}
EOF
done
}
function send_public_key() {
for vm in ${!VMS[@]}; do
sleep 1
expect <<EOF
spawn ssh-copy-id ${VMS[$vm]}
expect "assword" {send "${CLIENT_PWD}\r"}
expect "#" {send "echo\r"}
EOF
done
}
function main() {
rm -rf ~/.ssh/*
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
config_client
send_public_key
}
main