Viktor Kutuzov | a266b5f | 2009-07-14 20:08:45 +0000 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Program: RemoteRunSafely.sh |
| 4 | # |
| 5 | # Synopsis: This script simply runs another program remotely using rsh. |
| 6 | # It always returns the another program exit code. |
| 7 | # |
| 8 | # (?) only exit statuses that indicates that the program could not be executed |
| 9 | # normally is considered to indicate a test failure. |
| 10 | # |
| 11 | # Syntax: |
| 12 | # |
| 13 | # RemoteRunSafely.sh <hostname> [-l <login_name>] [-p <port>] |
| 14 | # <program> <args...> |
| 15 | # |
| 16 | # where: |
| 17 | # <hostname> is the remote host to execute the program, |
| 18 | # <login_name> is the username on the remote host, |
| 19 | # <port> is the port used by the remote client, |
| 20 | # <program> is the path to the program to run, |
| 21 | # <args...> are the arguments to pass to the program. |
| 22 | # |
| 23 | |
| 24 | printUsageAndExit() |
| 25 | { |
| 26 | echo "Usage:" |
| 27 | echo "./RemoteRunSafely.sh <hostname> [-l <login_name>] [-p <port>] " \ |
| 28 | "[cd <working_dir>] <program> <args...>" |
| 29 | exit 1 |
| 30 | } |
| 31 | |
| 32 | moreArgsExpected() |
| 33 | { |
| 34 | # $1 - remaining number of arguments |
| 35 | # $2 - number of arguments to shift |
| 36 | if [ $1 -lt $2 ] |
| 37 | then |
| 38 | echo "Error: Wrong number of argumants." |
| 39 | printUsageAndExit |
| 40 | fi |
| 41 | } |
| 42 | |
| 43 | # Save a copy of the original arguments in a string before we |
| 44 | # clobber them with the shift command. |
| 45 | ORIG_ARGS="$*" |
| 46 | #DEBUG: echo 'GOT: '$ORIG_ARGS |
| 47 | |
| 48 | moreArgsExpected $# 1 |
| 49 | RHOST=$1 |
| 50 | shift 1 |
| 51 | |
| 52 | RUSER=`id -un` |
| 53 | RCLIENT=ssh |
| 54 | RPORT= |
| 55 | WORKING_DIR= |
| 56 | |
| 57 | moreArgsExpected $# 1 |
| 58 | if [ $1 = "-l" ]; then |
| 59 | moreArgsExpected $# 2 |
| 60 | RUSER=$2 |
| 61 | shift 2 |
| 62 | fi |
| 63 | moreArgsExpected $# 1 |
| 64 | if [ $1 = "-p" ]; then |
| 65 | moreArgsExpected $# 2 |
| 66 | RPORT="-p $2" |
| 67 | shift 2 |
| 68 | fi |
| 69 | |
| 70 | moreArgsExpected $# 1 |
| 71 | PROGRAM=$(basename $1) |
| 72 | WORKING_DIR=$(dirname $1) |
| 73 | shift 1 |
| 74 | |
| 75 | #DEBUG: echo 'DIR='${0%%`basename $0`} |
| 76 | #DEBUG: echo 'RHOST='$RHOST |
| 77 | #DEBUG: echo 'RUSER='$RUSER |
| 78 | #DEBUG: echo 'PROGRAM='$PROGRAM |
| 79 | #DEBUG: echo 'WORKING_DIR='$WORKING_DIR |
| 80 | #DEBUG: echo 'ARGS='$* |
| 81 | |
| 82 | # Sanity check |
| 83 | if [ "$RHOST" = "" -o "$PROGRAM" = "" ]; then |
| 84 | printUsageAndExit |
| 85 | fi |
| 86 | |
| 87 | # Local program file must exist and be execuatble |
| 88 | local_program=$WORKING_DIR"/"$PROGRAM |
| 89 | if [ ! -x "$local_program" ]; then |
| 90 | echo "File "$local_program" does not exist or is not an executable.." |
| 91 | exit 2 |
| 92 | fi |
| 93 | |
| 94 | connection=$RUSER'@'$RHOST |
| 95 | remote="./"$PROGRAM |
| 96 | ( |
| 97 | cat $local_program | \ |
| 98 | $RCLIENT $connection $RPORT \ |
| 99 | 'rm -f '$remote' ; ' \ |
| 100 | 'cat > '$remote' ; chmod +x '$remote' ; '$remote' '$*' ; ' \ |
| 101 | 'echo exit $? ; ' \ |
| 102 | 'rm -f '$remote |
| 103 | ) |
| 104 | |
| 105 | #DEBUG: err=$? |
| 106 | #DEBUG: echo script exit $err |
| 107 | #DEBUG: exit $err |
| 108 | |