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