Operating System - HP-UX
1827852 Members
1622 Online
109969 Solutions
New Discussion

Need ability to run remote shell script, capture results and act on results as they are returned.

 
SOLVED
Go to solution
Jack C. Mahaffey
Super Advisor

Need ability to run remote shell script, capture results and act on results as they are returned.

I have the need for running a interactively remote shell script on a remote server and perform further processing on the local host in a loop.

Does anyone have such a critter.

I've tried a do while ... read filename done and it terminates after the first remsh executes.

I've also tried for n in VARIABLE_LIST ... and still can't capture the results and do further processing.


Script does the following:

Read a configuration file.

For the first argument in each row, do the following:
1 - execute remsh command
2 - Capture output from remote shell command.
3 - Depending on the results, send a file back to the local host.
4 - Do processing on the local host on the file created by the remote host.
5 - Process the nex entry from the configuration file.

Once all entries are processed, exit the loop.

I can't capture the exit code from the script executed by the remote shell. When I attempt to loop via the read command, the eof gets set after the first remsh execution even if there are more rows.

Any ideas will be appreciated.


Thanks... jack...



2 REPLIES 2
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Need ability to run remote shell script, capture results and act on results as they are returned.

Hi Jack:

That question has come up many times. I will simply duplicate one of my earlier posts.
NOTE: This is the most general solution to the problem when you also need STDOUT and STDERR from your remsh'ed process. i.e. This method works in all cases.

The remsh exit code of 0 only means that the script was able to be launched. The exit status of the remote command is not returned. There is a workaround I have used for a long time.

REM_ERR_FILE=/var/tmp/x${$}.err
remsh remote_host my_command $REM_ERR_FILE
LOCAL_STAT=$?

$LOCAL_STAT only tells us about the success of remsh itself

Now my_command on the remote host can get the
value of $REM_ERR_FILE; any status values you are interested in can be written to that file by your script on the remote host.

When the remsh command finishes you can then
REM_STAT=`remsh remote_host cat ${REM_ERR_FILE}`
to capture the remote commands status. (You should then issue a final remsh to remove the $REM_ERR_FILE.)

It's a little complicated but it does work. Since we generate a process id dependent filename on the local host, you don't have to worry about filename collision when multiple instances are running. This method also leaves stderr and stdout for their normal use.

You can serach the Forums for remsh and status for other methods.

Regards, Clay
If it ain't broke, I can fix that.
Chris Vail
Honored Contributor

Re: Need ability to run remote shell script, capture results and act on results as they are returned.

This is tricky, but I've done it. Here is one secret. Be sure to use quotation marks around your command:
remsh HOST "command">>OUTFILE.out 2>/dev/null

This writes the standard output from the remote hose to a file on the local host (outfile.out). Next process the output file, and remsh the remote system to take action on it.

It'll take a few tries to get it right. I use this methodology to run bdf on multiple systems, and then assemble the data into a spreadsheet for trending/planning purposes.

Chris