在自动化测试,或者Oracle RAC安装过程中,需要配置主机之间SSH 无密码访问,当有多台主机时,配置起来很麻烦,网上的很多类似脚本拿到本机有问题,所以自己写了一个:
-
#!/bin/bash
-
-
NUM_OF_NODES=
-
NODE1=""
-
NODE2=""
-
NODE3=""
-
NODE4=""
-
NODE5=""
-
-
USER=root
-
EXPECT=/usr/bin/expect
-
PASSWD=""
-
#USER_PROMPT="*$ "
-
USER_PROMPT="*# "
-
-
if [ "x${NODE1}" == "x" -o "x${USER}" == "x" -o "x${PASSWD}" == "x" ]; then
-
echo ""
-
echo "Please set the NODE INFO, USER and PASSWD"
-
echo "then $0 to start..."
-
exit 1
-
fi
-
-
declare -i l_i=1
-
while [ $l_i -le $NUM_OF_NODES ]
-
do
-
eval l_current_node=\$NODE$l_i
-
-
$EXPECT <<EOF
-
spawn ssh $USER@$l_current_node
-
expect "*(yes/no)?*" {
-
send -- "yes\r"
-
expect "*?assword:*"
-
send -- "$PASSWD\r"
-
} "*?assword:*" {send -- "$PASSWD\r"}
-
expect "$USER_PROMPT"
-
send -- "ssh-keygen -t rsa -q -f ~/.ssh/id_rsa -P '' \r"
-
expect "*Overwrite (yes/no)? " {
-
send -- "yes\r"
-
} "$USER_PROMPT" {send -- "\r"}
-
expect "$USER_PROMPT"
-
send -- "cat ~/.ssh/id_rsa.pub | ssh $USER@$NODE1 'cat - >> ~/.ssh/authorized_keys' \r"
-
expect "*(yes/no)?*" {
-
send -- "yes\r"
-
expect "*?assword:*"
-
send -- "$PASSWD\r"
-
} "*?assword:*" {send -- "$PASSWD\r"}
-
expect "$USER_PROMPT"
-
send -- "exit\r"
-
EOF
-
((l_i++))
-
done
-
-
declare -i l_n=1
-
while [ $l_n -le $NUM_OF_NODES ]
-
do
-
eval l_current_node=\$NODE$l_n
-
$EXPECT <<EOF
-
-
spawn ssh $USER@$NODE1
-
expect "*?assword:*" {
-
send -- "$PASSWD\r"
-
expect "$USER_PROMPT"
-
} "$USER_PROMPT" {send -- "scp ~/.ssh/authorized_keys $l_current_node:~/.ssh/ \r"}
-
expect "*?assword:*"
-
send -- "$PASSWD\r"
-
expect "$USER_PROMPT"
-
send -- "exit\r"
-
EOF
-
((l_n++))
-
done
阅读(4788) | 评论(0) | 转发(0) |