2、使用tnsnames方式轮巡多服务器及实例
#下面是通过tnsnames方式进行轮巡的shell脚本,也就是仅仅需要从远程服务器获取/etc/oratab下的所有实例即可
#需要注意的:
# a. 确保本地环境存在到所有远程服务器的tnsnames连接字符串,如果没有也没有关系,会收到错误
# b. 需要所有被轮巡数据库的用户及密码并具有相应的权限
# c. 如果所需要执行的sql属于比较复杂的情形,我们可以将sql语句写入到一个单独的文件,然后调用
oracle@linux1:~> more query_multi_inst_tns.sh
# ------------------------------------------------------------------------------------+
# Script Name: query_multi_inst_tns.sh |
# Desc: This script login to different remote host where define in known_host file, |
# after that look through oratab and execute SQL for each instance |
# in local machine by tnsnames. |
# Req: Configure a secure shell by ssh-keygen to all remote host |
# Add Oracle Net string to local tnsnames.ora for each remote instance |
# Author : Robinson |
# Blog : http://www.linuxidc.com |
# ------------------------------------------------------------------------------------+
#!/bin/bash
# --------------------------------------------
# Set environment vairable and define variable
# --------------------------------------------
if [ -f ~/.bash_profile ]; then
. ~/.bash_profile
fi
ORATAB=/etc/oratab
RHOST=~/.ssh/known_hosts
LOGFILE=/users/robin/dba_scripts/custom/log/query_multi_inst.log
# -------------------------------
# take a loop in each hostname
# -------------------------------
{
for host in `cat $RHOST | awk '{print $1}'`
do
echo "************************************"
echo "Current host is $host."
echo "************************************"
echo ""
# ---------------------------
# take a loop in ORATAB file
# ---------------------------
for db in `ssh $host cat $ORATAB |egrep ':N|:Y'|grep -v \* | grep -v \# |cut -d ":" -f1`
do
echo "------------------------------------"
echo "Current database is $db. "
echo "------------------------------------"
$ORACLE_HOME/bin/sqlplus -S usr/passwd@$db <<EOF
col name format a30
col value format a20
select name,value from v\$parameter where name='open_cursors';
exit
EOF
done
done
}|tee -a ${LOGFILE} 2>&1
exit
