brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.3 KiB · 302c0db Raw
106 lines · bash
1#!/bin/sh2#3# Program:  RemoteRunSafely.sh4#5# Synopsis: This script simply runs another program remotely using ssh.6#           It always returns the another program exit code or exit with7#           code 255 which indicates that the program could not be executed.8#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>] " \26       "<program> <args...>"27  exit 25528}29 30moreArgsExpected()31{32  # $1 - remaining number of arguments33  # $2 - number of arguments to shift34  if [ $1 -lt $2 ]35  then36    echo "Error: Wrong number of arguments."37    printUsageAndExit38  fi39}40 41# Save a copy of the original arguments in a string before we42# clobber them with the shift command.43ORIG_ARGS="$*"44#DEBUG: echo 'GOT: '$ORIG_ARGS45 46moreArgsExpected $# 147RHOST=$148shift 149 50RUSER=`id -un`51RCLIENT=ssh52RPORT=53WORKING_DIR=54 55moreArgsExpected $# 156if [ $1 = "-l" ]; then57  moreArgsExpected $# 258  RUSER=$259  shift 260fi61moreArgsExpected $# 162if [ $1 = "-p" ]; then63  moreArgsExpected $# 264  RPORT="-p $2"65  shift 266fi67 68moreArgsExpected $# 169PROGRAM=$(basename $1)70WORKING_DIR=$(dirname $1)71shift 172 73#DEBUG: echo 'DIR='${0%%`basename $0`}74#DEBUG: echo 'RHOST='$RHOST75#DEBUG: echo 'RUSER='$RUSER76#DEBUG: echo 'PROGRAM='$PROGRAM77#DEBUG: echo 'WORKING_DIR='$WORKING_DIR78#DEBUG: echo 'ARGS='$*79 80# Sanity check81if [ "$RHOST" = "" -o "$PROGRAM" = "" ]; then82  printUsageAndExit83fi84 85# Local program file must exist and be execuatble86local_program=$WORKING_DIR"/"$PROGRAM87if [ ! -x "$local_program" ]; then88  echo "File "$local_program" does not exist or is not an executable.."89  exit 25590fi91 92connection=$RUSER'@'$RHOST93remote="./"$PROGRAM94(95  cat $local_program |        \96  $RCLIENT $connection $RPORT \97   'rm -f '$remote' ; '       \98   'cat > '$remote' ; chmod +x '$remote' ; '$remote' '$*' ; ' \99   'err=$? ; rm -f '$remote' ; exit $err'100)101err=$?102 103#DEBUG: echo script exit $err104exit $err105 106