#!/bin/sh
DEST_USER=$1
PASSWORD=$2
HOSTS_FILE=$3
if [ $# -ne 3 ]; then
echo “Usage:”
echo “$0 remoteUser remotePassword hostsFile”
exit 1
fi
SSH_DIR=~/.ssh
SCRIPT_PREFIX=./tmp
echo ===========================
-
prepare directory .ssh
mkdir $SSH_DIR
chmod 700 $SSH_DIR -
generat ssh key
TMP_SCRIPT=KaTeX parse error: Expected 'EOF', got '#' at position 25: …EFIX.sh echo "#̲!/usr/bin/expec…TMP_SCRIPT
echo “spawn ssh-keygen -b 1024 -t rsa”>> T M P S C R I P T e c h o " e x p e c t ∗ k e y ∗ " > > TMP_SCRIPT echo "expect *key*">> TMPSCRIPTecho"expect∗key∗">>TMP_SCRIPT
echo “send \r”>>$TMP_SCRIPT
if [ -f S S H D I R / i d r s a ] ; t h e n e c h o " e x p e c t ∗ v e r w r i t e ∗ " > > SSH_DIR/id_rsa ]; then echo "expect *verwrite*">> SSHDIR/idrsa];thenecho"expect∗verwrite∗">>TMP_SCRIPT
echo “send y\r”>> T M P S C R I P T f i e c h o " e x p e c t ∗ p a s s p h r a s e ∗ " > > TMP_SCRIPT fi echo "expect *passphrase*">> TMPSCRIPTfiecho"expect∗passphrase∗">>TMP_SCRIPT
echo “send \r”>> T M P S C R I P T e c h o " e x p e c t ∗ a g a i n : " > > TMP_SCRIPT echo "expect *again:">> TMPSCRIPTecho"expect∗again:">>TMP_SCRIPT
echo “send \r”>> T M P S C R I P T e c h o " i n t e r a c t " > > TMP_SCRIPT echo "interact">> TMPSCRIPTecho"interact">>TMP_SCRIPT
chmod +x $TMP_SCRIPT
/usr/bin/expect $TMP_SCRIPT
rm $TMP_SCRIPT
- generat file authorized_keys
cat S S H D I R / i d r s a . p u b > > SSH_DIR/id_rsa.pub>> SSHDIR/idrsa.pub>>SSH_DIR/authorized_keys
#4. chmod 600 for file authorized_keys
chmod 600 $SSH_DIR/authorized_keys
echo ===========================
-
copy all files to other hosts
for ip in $(cat H O S T S F I L E ) d o i f [ " x HOSTS_FILE) do if [ "x HOSTSFILE)doif["xip" != “x” ]; then
echo -------------------------
TMP_SCRIPT= S C R I P T P R E F I X . {SCRIPT_PREFIX}. SCRIPTPREFIX.ip.sh
# check known_hosts
val=ssh-keygen -F $ip
if [ “x v a l " = = " x " ] ; t h e n e c h o " val" == "x" ]; then echo " val"=="x"];thenecho"ip not in $SSH_DIR/known_hosts, need to add”
val=ssh-keyscan $ip 2>/dev/null
if [ “x$val” == “x” ]; then
echo “ssh-keyscan $ip failed!”
else
echo v a l > > val>> val>>SSH_DIR/known_hosts
fi
fi
echo “copy $SSH_DIR to $ip”echo "#!/usr/bin/expect">$TMP_SCRIPT echo "spawn scp -r $SSH_DIR $DEST_USER@$ip:~/">>$TMP_SCRIPT echo "expect *assword*">>$TMP_SCRIPT echo "send $PASSWORD\r">>$TMP_SCRIPT echo "interact">>$TMP_SCRIPT chmod +x $TMP_SCRIPT #echo "/usr/bin/expect $TMP_SCRIPT" >$TMP_SCRIPT.do #sh $TMP_SCRIPT.do& /usr/bin/expect $TMP_SCRIPT rm $TMP_SCRIPT echo "copy done."
fi
done
echo done.
##################以上为 ssh_auth.sh脚本
##########配置互信主机ip列表
echo"192.168.10.202
192.168.10.203
192.168.10.205
192.168.10.206
192.168.10.207
192.168.10.208">>hostlist
#######################需安装expect及tcl依赖包
链接:https://pan.baidu.com/s/1xxd4kednpoQ6wzXEymPzgg
提取码:0q7o
tar -zxvf tcl8.4.11-src.tar.gz
cd tcl8.4.11/unix/ &&./configure && make && make install
tar -zvxf expect-5.43.0.tar.gz
cd expect-5.43.0 && ./configure --with-tclinclude=/usr/local/src/tcl8.4.11/generic --with-tclconfig=/usr/local/lib/ && make && make install
expect
ln -s /usr/local/bin/expect /usr/bin/expect
ll /usr/bin/expect
#######################脚本和hostlist文件放置同一路径并执行以下语句
sh ssh_auth.sh root passwd hostlist
###########################################################
###########################################################
##################两台服务器双机互信
节点1操作
su - grid
$ ssh-keygen -t rsa
$ ssh-keygen -t dsa
cat .ssh/id_rsa.pub >>.ssh/authorized_keys
cat .ssh/id_dsa.pub >>.ssh/authorized_keys
ssh dbrac2 cat .ssh/id_rsa.pub >>.ssh/authorized_keys
ssh dbrac2 cat .ssh/id_dsa.pub >>.ssh/authorized_keys
scp .ssh/authorized_keys dbrac2:.ssh/authorized_keys
节点2操作
su - grid
$ ssh-keygen -t rsa
$ ssh-keygen -t dsa
cat .ssh/id_rsa.pub >>.ssh/authorized_keys
cat .ssh/id_dsa.pub >>.ssh/authorized_keys
ssh dbrac1 cat .ssh/id_rsa.pub >>.ssh/authorized_keys
ssh dbrac1 cat .ssh/id_dsa.pub >>.ssh/authorized_keys
scp .ssh/authorized_keys dbrac1:.ssh/authorized_keys
##############################################################
#############################################################