快速做所有主机相互认证

脚本:

#!/usr/bin/expect
spawn sensors-detect
for {set i 0} {$i<=10} {incr i} {
expect ":"
send "\n"
}
interact

解释:

spawn是启动命令
for为循环的写法
interact为退出
这个脚本目的是在一次运行过程中不断模拟用户的enter操作

设置免密码交互

#!/bin/bash

if [ ! -f "/root/.ssh/id_rsa" ]; then

/usr/bin/expect <<-EOF
spawn ssh-keygen
expect {
"*save the key (/root/.ssh/id_rsa" { send "\n"; exp_continue }
"*(empty for no passphrase)" { send "\n"; exp_continue }
"*same passphrase again" { send "\n";}
}
interact
expect eof
EOF
fi


for host in `cat /etc/hosts|awk '{print $1}'`
do

passwd="ubuntu"
/usr/bin/expect <<-EOF
spawn ssh-copy-id root@$host

expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "$passwd\n" }
" All keys were skipped because they already exist on the remote system." {send "\n"}
}
interact
expect eof
EOF

done

把host文件传到所有机器
先从第一台主机做做配置跟其他机器的交互
然后seq把脚本传递到其他所有机器
然后用seq 循环ssh 远程执行脚本

变更记录

Why Who When
创建 武汉-运维-磨渣 2013-8-12
更新 武汉-运维-磨渣 2015-3-23
更新所有主机交互 武汉-运维-磨渣 2019-3-29