Running remote shell script containing sql statements

I have a shell script which resides on three SCO machines containing some simple sqlplus statments. I need to run these scripts remotely. Currently, I am trying to use rsh to do so:

rsh hostname myscript args

The problem is that the arguments to the sqlplus statements in the remote shell scripts are unrecognized. If I run the scripts from the command line of their respective machines, everything works fine. So, it looks like an rsh issue. Here is some more specific information:

I am running rsh as follows:

rsh raptor bin/my_script Y Y Y Y

where the Ys are the args to my_script. my_script contains the following:

#!/usr/bin/ksh

which_sql=`whence sqlplus`

$which_sql -s sti/sti <<EOF
set heading off
set pause off
update my_table
set HOST_ON_DUTY = '$1',
EXPRESS_SHIPPING = '$2',
INTERNATIONAL_ORDERS= '$3',
COD = '$4';
exit
EOF

..and the error I'm getting back is:

bin/my_script: line 5: -s: not found

..I am guessing that the flags to the sql statement are being interpreted locally instead of remotely.

Any help would be greatly appreciated!

rsh does not execute your .profile (same as ftp), so your environment is not getting established like it would on a true login to that server. With ORACLE_HOME/bin not in your PATH, the whence is coming up null. Since $which_sql is null, the first thing on the command line is the -s.

First thing in my_script should be to source your environment so that sqlplus is in your PATH.

I suggest you to create a remote shell script. I'm not use RSH anymore since it's not a secure connection type.

Like Jimbo, i suggest you to define the environtment first, for example:

-------------------------------------

#!/bin/sh

PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin

... your shell code here.

-------------------------------------

write you script and test on the remote host.

copy your script to home.
when your are on your home again use:
cat myscript.sh | ssh remote "/bin/bash -s"

'bash -s' takes stdin as input (here the pipe), geuss ksh knows something like that. you earn extra bonus points by using reocde in cases when the charset on home and remote are different.

This type of stuff seems to just keep coming back. People want to run scripts remotely. I've tried every way I can think of and this is the best way. You not only get functionality but you get security as well. Every Unix environment needs a way to issue shell scripts, perl script, SQL scripts to remote servers. I have posted the following example already but I will do it again. Everytime I go to a new company the first thing I do is build an issuance process. A way to issue script remotely outside of using some scheduler like Maestro or Control -m.

This is really a multi step process. You need to setup SSH correctly and create a key pair and passphrase. Then you need to load your keys into the SSH agent so that you don't get prompted for a passphrase. The SSH agent will handle passing your passphrase to the other systems.

Example:

#ssh-add
Adding identity: /home/tester/.ssh2/id_dsa_1024_defender.pub
Need passphrase for /home/tester/.ssh2/id_dsa_1024_defender.pub
(1024-bit dsa, tester@defender, Sun Apr 30 2000 21:39:13).
Enter passphrase:

#ssh server date
Wed May 10 14:06:57 EDT 2006
#

#Now that you have SSH setup. You can take any script and scp the script to the remote server.

Sample script below
###############################################
#!/bin/sh

LOG="/tmp/MYTEST.log
##################
function if_error
##################
{
if [[ $? -ne 0 ]]; then # check return code passed to function
print "$1 TIME:$TIME" | tee -a $LOG # if rc > 0 then print error msg and quit
exit $?
fi
}

chown root:root /etc/passwd
if_error "Failed chowning /etc/passwd to root:root"

chmod 700 /etc/passwd
if_error "Failed chmoding /etc/passwd to 700"

rm -rf /etc/somedir
if_error "Failed deleting /etc/somedir"
################################################

#Okay..So SSH is working and you got a little test script with the proper error checking in it. Also, I used the tee command to redirect the output of the script to the screen and to a log file in tmp.

scp $USER@$HOST:$PATH/$SCRIPT $USER@$HOST:/tmp/$SCRIPT
ssh -l $USER $HOST chmod 700 /tmp/$SCRIPT
ssh -l $USER $HOST /tmp/$SCRIPT
ssh -l $USER HOST rm /tmp/$SCRIPT

  1. Copied script to server
  2. Changed permissions
  3. Executed script that will write to log file and screen cause I wrote it like that.
  4. rm the script

Hope this is helpful.

-X