#!/bin/sh
#### Author: Tangjun Chen
#### RHCE
#### Date: Wed Apr 11 16:24:25 CST 2012
#### Nodes Equivalent For RAC

sub_echo_red(){
  echo -e "\033[31;5;1m${1}\033[0m"
}
sub_echo_yellow(){
  echo -e "\033[33;5;1m${1}\033[0m"
}
sub_echo_blue(){
  echo -e "\033[34;5;1m${1}\033[0m"
}
sub_echo_green(){
  echo -e "\033[32;5;1m${1}\033[0m"
}


####please login by oracle
v_oracle=$( id | grep oracle | cut -c 1-1  )
if [ "$v_oracle" == '' ]
then
  sub_echo_red "you must login by oracle!!!"
exit
fi


####begin
sub_echo_red "assure you have /etc/hosts or DNS support the hostname you want."
read -p "please input the source hostname like stationx : " source_host
read -p "please input the target hostname like stationx : " target_host

v_check1=$( ls /home/oracle/.ssh/authorized_keys )
if [ "$v_check1" == '/home/oracle/.ssh/authorized_keys' ]
then
    sub_echo_red "you already had file /home/oracle/.ssh/authorized_keys "
else
    sub_echo_red "begin create equivalent for both nodes"
    mkdir -p /home/oracle/.ssh/OB
    touch /home/oracle/.ssh/authorized_keys
fi

v_check0=$( grep $source_host /home/oracle/.ssh/authorized_keys | cut -c 1-1 )
if [ "$v_check0" == '' ]
then
###generate public key and private key respectively.
ssh $target_host  "/usr/bin/ssh-keygen -t dsa"
scp $target_host:/home/oracle/.ssh/id_dsa.pub   /home/oracle/.ssh/authorized_keys.tmp

/usr/bin/ssh-keygen -t dsa
cat /home/oracle/.ssh/id_dsa.pub  > /home/oracle/.ssh/authorized_keys
cat /home/oracle/.ssh/authorized_keys.tmp >> /home/oracle/.ssh/authorized_keys
scp /home/oracle/.ssh/authorized_keys $target_host:/home/oracle/.ssh/authorized_keys
rm -rf /home/oracle/.ssh/authorized_keys.tmp


echo ""
echo ""
sub_echo_red "First time login,you must do it manunally!!!!"
sub_echo_red "Login target_host run "ssh \$source_host" "
sub_echo_red "such as station3-vip, station3-priv, station3"
sub_echo_red "        station4-vip, station4-priv, station4"
sub_echo_red "Run in both nodes: "
sub_echo_red "$source_host:  "
sub_echo_red "  ssh $source_host , ssh $source_host-priv , ssh $source_host-vip"
sub_echo_red "  ssh $target_host , ssh $target_host-priv , ssh $target_host-vip"
sub_echo_red "$target_host:  "
sub_echo_red "  ssh $source_host , ssh $source_host-priv , ssh $source_host-vip"
sub_echo_red "  ssh $target_host , ssh $target_host-priv , ssh $target_host-vip"

####source_host: ssh both source_host and target_host
ssh $source_host  exit   
ssh $source_host-priv  exit
ssh $source_host-vip  exit
ssh $target_host  exit   
ssh $target_host-priv  exit
ssh $target_host-vip  exit

####target_host: ssh both source_host and target_host
cat > /home/oracle/.ssh/tempfile01.sh << EOF
#!/bin/sh
ssh $source_host  exit
ssh $source_host-priv  exit
ssh $source_host-vip  exit
ssh $target_host  exit
ssh $target_host-priv  exit
ssh $target_host-vip  exit
EOF
chmod +x /home/oracle/.ssh/tempfile01.sh
scp -p /home/oracle/.ssh/tempfile01.sh $target_host:/home/oracle/.ssh/tempfile01.sh
rm -rf /home/oracle/.ssh/tempfile01.sh
sub_echo_red "please login station4 run /home/oracle/.ssh/tempfile01.sh"
sleep 10

 

else

echo ""
echo ""
sub_echo_red "First time login,you must do it manunally!!!!"
sub_echo_red "Login target_host run "ssh \$source_host" "
sub_echo_red "such as station3-vip, station3-priv, station3"
sub_echo_red "        station4-vip, station4-priv, station4"
sub_echo_red "Run in both nodes: "
sub_echo_red "$source_host:  "
sub_echo_red "  ssh $source_host , ssh $source_host-priv , ssh $source_host-vip"
sub_echo_red "  ssh $target_host , ssh $target_host-priv , ssh $target_host-vip"
sub_echo_red "$target_host:  "
sub_echo_red "  ssh $source_host , ssh $source_host-priv , ssh $source_host-vip"
sub_echo_red "  ssh $target_host , ssh $target_host-priv , ssh $target_host-vip"

####source_host: ssh both source_host and target_host
ssh $source_host  exit
ssh $source_host-priv  exit
ssh $source_host-vip  exit
ssh $target_host exit
ssh $target_host-priv  exit
ssh $target_host-vip  exit

####target_host: ssh both source_host and target_host
cat > /home/oracle/.ssh/tempfile01.sh << EOF
#!/bin/sh
ssh $source_host  exit
ssh $source_host-priv  exit
ssh $source_host-vip  exit
ssh $target_host  exit
ssh $target_host-priv  exit
ssh $target_host-vip  exit
EOF
chmod +x /home/oracle/.ssh/tempfile01.sh
scp -p /home/oracle/.ssh/tempfile01.sh $target_host:/home/oracle/.ssh/tempfile01.sh
rm -rf /home/oracle/.ssh/tempfile01.sh
sub_echo_red "please login station4 run /home/oracle/.ssh/tempfile01.sh"
sleep 10

fi