First, setup ssh public key authentication to login to remote servers (after setup) without prompt. Do this as the oracle OS user.
http://superuser.com/questions/8077/how-do-i-set-up-ssh-so-i-dont-have-to-type-my-password
Second, run (ymmv):
ps -ef | grep ora_smon | grep -v grep | grep -v awk | awk -F'ora_smon_' '{ print $2 }'
Third, wrap this in a shell for loop:
for sid in `ps -ef | grep ora_smon | grep -v grep | grep -v awk | awk -F'ora_smon_' '{ print $2 }'`; do
EXECUTE_SCRIPT_BELOW;
done
Fourth, run this script for each instance to setup the run of your SQL script:
PATH=/usr/local/bin:$PATH
ORAENV_ASK=NO
ORACLE_SID=$sid
export PATH
export ORAENV_ASK
export ORACLE_SID
. /usr/local/bin/oraenv
TWO_TASK=
export TWO_TASK
sqlplus / as sysdba @your_sql_script.sql
There are a lot of assumptions here, but if your oracle environment is close to default you should be ok:
"sqlplus / as sysdba" only works if the "dba" os group is assigned to the user you are logged in as via ssh.
It also assumes that a running instance is also "open" for your script, which may not be correct.
It also assumes the oracle environment script is in /usr/local/bin.