あなたが持っているスクリプトの複雑さによって異なります。 rpcsh機能を使用して、スクリプトからシェル関数をリモートで実行できるように、書き直したい場合があります。あなたはこれがあなたにそのリモートホスト上の〜/ sample_rpcsh.txtファイルを与えるスクリプト
#!/bin/sh
source /usr/local/include/rpcsh.inc
MASTER_ARG=""
function ahelper() {
# used by doremotely just to show that we can
echo "master arg $1 was passed in"
}
function doremotely() {
# this executes on the remote host
ahelper $MASTER_ARG > ~/sample_rpcsh.txt
}
# main
MASTER_ARG="newvalue"
# send the function(s) and variable to the remote host and then execute it
rpcsh -u user -h host -f "ahelper doremotely" -v MASTER_ARG -r doremotely
を持つことができます(例えば)/usr/local/include/rpcsh.incに保存https://gist.github.com/Shadowfen/2b510e51da6915adedfbを使用して
rpcsh.incの
master arg newvalue was passed in
コピーが含まれています(ケース・リンクに悪い行く):
#!/bin/sh
# create an inclusion guard (to prevent multiple inclusion)
if [ ! -z "${RPCSH_GUARD+xxx}" ]; then
# already sourced
return 0
fi
RPCSH_GUARD=0
# rpcsh -- Runs a function on a remote host
# This function pushes out a given set of variables and functions to
# another host via ssh, then runs a given function with optional arguments.
# Usage:
# rpcsh -h remote_host -u remote_login -v "variable list" \
# -f "function list" -r mainfunc [-- param1 [param2]* ]
#
# The "function list" is a list of shell functions to push to the remote host
# (including the main function to run, and any functions that it calls).
#
# Use the "variable list" to send a group of variables to the remote host.
#
# Finally "mainfunc" is the name of the function (from "function list")
# to execute on the remote side. Any additional parameters specified (after
# the --)gets passed along to mainfunc.
#
# You may specify multiple -v "variable list" and -f "function list" options.
#
# Requires that you setup passwordless access to the remote system for the script
# that will be running this.
rpcsh() {
if ! args=("$(getopt -l "host:,user:,pushvars:,pushfuncs:,run:" -o "h:u:v:f:r:A" -- "[email protected]")")
then
echo getopt failed
logger -t ngp "rpcsh: getopt failed"
exit 1
fi
sshvars=(-q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null)
eval set -- "${args[@]}"
pushvars=""
pushfuncs=""
while [ -n "$1" ]
do
case $1 in
-h|--host) host=$2;
shift; shift;;
-u|--user) user=$2;
shift; shift;;
-v|--pushvars) pushvars="$pushvars $2";
shift; shift;;
-f|--pushfuncs) pushfuncs="$pushfuncs $2";
shift; shift;;
-r|--run) run=$2;
shift; shift;;
-A) sshvars=("${sshvars[@]}" -A);
shift;;
-i) sshvars=("${sshvars[@]}" -i $2);
shift; shift;;
--) shift; break;;
esac
done
remote_args=("[email protected]")
vars=$([ -z "$pushvars" ] || declare -p $pushvars 2>/dev/null)
ssh ${sshvars[@]} ${user}@${host} "
#set -x
$(declare -p remote_args)
$vars
$(declare -f $pushfuncs)
$run ${remote_args[@]}
"
}