blob: 543a22554ea056bb078e361dc3171ab6e8bb26cd [file] [log] [blame]
Viktor Kutuzova266b5f2009-07-14 20:08:45 +00001#!/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
24printUsageAndExit()
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
32moreArgsExpected()
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.
45ORIG_ARGS="$*"
46#DEBUG: echo 'GOT: '$ORIG_ARGS
47
48moreArgsExpected $# 1
49RHOST=$1
50shift 1
51
52RUSER=`id -un`
53RCLIENT=ssh
54RPORT=
55WORKING_DIR=
56
57moreArgsExpected $# 1
58if [ $1 = "-l" ]; then
59 moreArgsExpected $# 2
60 RUSER=$2
61 shift 2
62fi
63moreArgsExpected $# 1
64if [ $1 = "-p" ]; then
65 moreArgsExpected $# 2
66 RPORT="-p $2"
67 shift 2
68fi
69
70moreArgsExpected $# 1
71PROGRAM=$(basename $1)
72WORKING_DIR=$(dirname $1)
73shift 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
83if [ "$RHOST" = "" -o "$PROGRAM" = "" ]; then
84 printUsageAndExit
85fi
86
87# Local program file must exist and be execuatble
88local_program=$WORKING_DIR"/"$PROGRAM
89if [ ! -x "$local_program" ]; then
90 echo "File "$local_program" does not exist or is not an executable.."
91 exit 2
92fi
93
94connection=$RUSER'@'$RHOST
95remote="./"$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