Metawerx Java Hosting Small Logo

remote_script.sh - Execute local scripts remotely over SSH

This script allows a local script to be executed over a keyfile-authenticated SSH connection.

This script may be updated from time-to-time. The current version is listed at the top of the script.

#!/bin/bash

# remote_script.sh - version 1.0.1 - (c) Neale Rudd, 2011
# 
# This script allows a local script to be executed over a keyfile-authenticated SSH connection.
#
# Run script remotely
#
# Usage:
# sh remote_script.sh -methodarg keyfile user host port script1 [script2] [script3] [script4]
#
# History:
# ------------------------------------------------------------------------------------------------
# 9/10/2011 1.0.1 Fix bug in -args method
# 9/10/2011 1.0.0 Initial Version
# ------------------------------------------------------------------------------------------------

if [ "$1" != "-direct" -a "$1" != "-temp" -a "$1" != "-args" ]; then
	BADARGS=true
elif [ "$1" = "" -o "$2" = "" -o "$3" = "" -o "$4" = "" -o "$5" = "" -o "$6" = "" ]; then
	BADARGS=true
else
	BADARGS=false
fi

if [ "$BADARGS" = "true" ]; then
	echo remote_script.sh - \(c\) Neale Rudd, 2011
	echo 
	echo This script allows a local script to be executed over a keyfile-authenticated SSH connection.
	echo
	echo Usage:
	echo 
	echo Method: -direct
	echo Run 1 or more local scripts remotely, directly without temp storage on the remote server
	echo sh remote_script.sh -direct keyfile user host port script \[script2\] \[script3\] \[script4\]
	echo
	echo Method: -temp
	echo Run 1 or more local scripts remotely, temporarily storing the script into /dev/shm
	echo sh remote_script.sh -temp keyfile user host port script \[script2\] \[script3\] \[script4\]
	echo
	echo Method: -args
	echo Run a local script remotely, with up to 3 arguments
	echo sh remote_script.sh -args keyfile user host port script \[arg1\] \[arg2\] \[arg3\]
	echo
	exit 1
fi

METHOD=$1
KEYFILE=$2
USER=$3
HOST=$4
PORT=$5
SCRIPT1=$6

SCRIPTRAND=$RANDOM
if [ "$SCRIPTRAND" = "" ]; then
	# Use alternate method
	SCRIPTRAND=`hexdump -n 2 -e '/2 "%u"' /dev/urandom`
	if [ "$SCRIPTRAND" = "" ]; then
		echo Error: Can\'t use \$RANDOM, and can\'t get random bytes from /dev/urandom!  Please run script with bash instead of dash.
		exit 1
	fi
fi

if [ "$METHOD" = "-direct" ]; then
	# Run 1 or more local scripts remotely, directly without temp storage on the remote server
	SCRIPT2=$7
	SCRIPT3=$8
	SCRIPT4=$9
	cat $SCRIPT1 $SCRIPT2 $SCRIPT3 $SCRIPT4 | ssh -C -p $PORT -i $KEYFILE $USER@$HOST
elif [ "$METHOD" = "-temp" ]; then
	# Run 1 or more local scripts remotely, temporarily storing the script into /dev/shm
	SCRIPT2=$7
	SCRIPT3=$8
	SCRIPT4=$9
	cat $SCRIPT1 $SCRIPT2 $SCRIPT3 $SCRIPT4 | ssh -C -p $PORT -i $KEYFILE $USER@$HOST "rm /dev/shm/rnd* 2>/dev/null; echo SCRIPTRAND=/dev/shm/rnd$SCRIPTRAND; cat >/dev/shm/rnd$SCRIPTRAND; sh /dev/shm/rnd$SCRIPTRAND; rm /dev/shm/rnd$SCRIPTRAND"
elif [ "$METHOD" = "-args" ]; then
	# Run a local script remotely, with up to 3 arguments
	ARG1=$7
	ARG2=$8
	ARG3=$9
	cat $SCRIPT1 | ssh -C -p $PORT -i $KEYFILE $USER@$HOST "rm /dev/shm/rnd* 2>/dev/null; cat >/dev/shm/rnd$SCRIPTRAND; sh /dev/shm/rnd$SCRIPTRAND $ARG1 $ARG2 $ARG3; rm /dev/shm/rnd$SCRIPTRAND;"
fi

navigation
metawerx specific
search
Share
tools
help

referring pages

Share