Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 E-mail Us
 Oracle Articles
New Oracle Articles

 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog

 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Oracle Support

 SQL Tuning

 Oracle UNIX
 Oracle Linux
 Remote s
 Remote plans
 Application Server

 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S


 Consulting Staff
 Consulting Prices
 Help Wanted!


 Oracle Posters
 Oracle Books

 Oracle Scripts

Don Burleson Blog 







Script to query multiple Oracle servers & instances

Oracle Database Tips by Donald BurlesonMay 7, 2015

I need to have a script that runs the same query on all of my instances.  I have 30 instances that reside on four different servers.  How do I write a script that will run on multiple servers from a single point?

Answer:  In a secure environment, you can open-up the remote shell (rsh) feature to hop between servers, and the hope between instances to run a single query on many instances.

Looping between UNIX/Linux Servers

UNIX/Linux provides a for command that can be used to loop for a specific number of iterations. When the for command is used to read the /etc/hosts and /etc/oratab files, you can write a script that loops from server-to-server, and database-to-database.

This is especially useful where all UNIX servers are "trusted", by allowing remote shell (rsh) commands.  This is done by making an entry in the .rhosts file for your ORACLE UNIX user. This is called a nested loop construct, and is illustrated my the pseudocode below.

FOR every server defined in .rhosts
     FOR every database defined in the server's /etc/oratab
            connect to SQL*Plus

This is a very powerful construct, and it can be used to visit every database in your enterprise. In the following example, we perform a double loop, looping from server to server.  For each server, we loop from database to database. Here is the actual UNIX code that perform the loops.

# Loop through each host name . . .
for host in `cat ~oracle/.rhosts|cut -d"." -f1|awk '{print $1}'|sort -u`
  echo " "
  echo "************************"
  echo "$host"
  echo "************************"
  # Loop through each database name on the host /etc/oratab . . .
  for db in `rsh $host "cat /etc/oratab|egrep ':N|:Y'|grep -v \*|\
  cut -f1 -':'"`

   see code depot for full script
     # Get the ORACLE_HOME for each database
     home=`rsh $host "cat /etc/oratab|egrep ':N|:Y'|grep -v \*|\
     grep {db}|cut -f2 -d':'"`
     echo " "
     echo "database is $db"
     sqlplus system/manager@$db <<!
     select * from v\$database;

In the example above we connected to SQL*Plus using a Net8 connect string for the remote database.  However, we can also use the UNIX rsh command to directly connect to the SQL*Plus facility on the remote UNIX server.Let's take a look at how this works.

Executing SQL*Plus on all remote UNIX servers

We can expand upon this approach and write a script to visit SQL*Plus on every server and every database on each server.  In the example below, we select the name of every database on every server, and display the optimizer_mode for every database.

# Loop through each host name . . .
for host in `cat ~oracle/.rhosts|cut -d"." -f1|awk '{print $1}'|sort -u`
    echo " "
    echo "************************"
    echo "$host"
    echo "************************"
    # loop from database to database
    for db in `cat /etc/oratab|egrep ':N|:Y'|grep -v \*|\
    grep ${db}|cut –f1 -':'"`
    see code depot for full script
        home=`rsh $host "cat /etc/oratab|egrep ':N|:Y'|\
        grep -v \*|grep {db}|cut -f2 -d':'
        "`echo "************************"
        echo "database is $db"
        echo "************************"
        rsh $host "
        ORACLE_SID=${db}; export ORACLE_SID;
        ORACLE_HOME=${home}; export ORACLE_HOME;
        ${home}/bin/sqlplus -s /<<!
        set pages 9999;
        set heading off;
        select value from v"\\""$"parameter where name='optimizer_mode';

In this example, we can quickly get a report of the Oracle optimizer_mode value for all databases in out enterprise.  This can make tasks that might take hours into a simple task that takes just a few seconds.
Get the Complete
Oracle SQL Tuning Information 

The landmark book "Advanced Oracle SQL Tuning  The Definitive Reference"  is filled with valuable information on Oracle SQL Tuning. This book includes scripts and tools to hypercharge Oracle 11g performance and you can buy it for 30% off directly from the publisher.



Oracle Training at Sea
oracle dba poster

Follow us on Twitter 
Oracle performance tuning software 
Oracle Linux poster


Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


Copyright © 1996 -  2020

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.