在远程机器上执行本地脚本
Executing a local script on a remote Machine
我的本地机器上有一个脚本,但需要在远程机器上 运行 它而不是将它复制到那里(IE,我不能通过 sftp 传输它,只是 运行 它那里)
我目前有以下功能命令
echo 'cd /place/to/execute' | cat - test.sh | ssh -T user@hostname
但是,我还需要为 test.sh 提供一个命令行参数。
我尝试将它添加到 .sh 之后,就像我在本地执行时所做的那样,但这没有用:
echo 'cd /place/to/execute' | cat - test.sh "arg" | ssh -T user@hostname
"cat: arg: No such file or directory" 是产生的错误
您需要覆盖参数:
echo 'set -- arg; cd /place/to/execute' | cat - test.sh | ssh -T user@hostname
以上将第一个参数设置为arg
。
一般:
set -- arg1 arg2 arg3
将覆盖 bash 中的 </code>、<code>
、</code>。</p>
<p>这基本上会使 <code>cat - test.sh
的结果成为一个不需要任何参数的独立脚本。
取决于您拥有的脚本的复杂程度。您可能希望重写它以便能够使用 rpcsh 功能从您的脚本远程执行 shell 函数。
使用 https://gist.github.com/Shadowfen/2b510e51da6915adedfb 保存到 /usr/local/include/rpcsh.inc(例如)你可以有一个脚本
#!/bin/sh
source /usr/local/include/rpcsh.inc
MASTER_ARG=""
function ahelper() {
# used by doremotely just to show that we can
echo "master arg 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
这将在包含
的远程主机上为您提供一个 ~/sample_rpcsh.txt 文件
master arg newvalue was passed in
rpcsh.inc 的副本(以防 link 变坏):
#!/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" -- "$@")")
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 "" ]
do
case in
-h|--host) host=;
shift; shift;;
-u|--user) user=;
shift; shift;;
-v|--pushvars) pushvars="$pushvars ";
shift; shift;;
-f|--pushfuncs) pushfuncs="$pushfuncs ";
shift; shift;;
-r|--run) run=;
shift; shift;;
-A) sshvars=( "${sshvars[@]}" -A );
shift;;
-i) sshvars=( "${sshvars[@]}" -i );
shift; shift;;
--) shift; break;;
esac
done
remote_args=( "$@" )
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[@]}
"
}
我的本地机器上有一个脚本,但需要在远程机器上 运行 它而不是将它复制到那里(IE,我不能通过 sftp 传输它,只是 运行 它那里)
我目前有以下功能命令
echo 'cd /place/to/execute' | cat - test.sh | ssh -T user@hostname
但是,我还需要为 test.sh 提供一个命令行参数。
我尝试将它添加到 .sh 之后,就像我在本地执行时所做的那样,但这没有用:
echo 'cd /place/to/execute' | cat - test.sh "arg" | ssh -T user@hostname
"cat: arg: No such file or directory" 是产生的错误
您需要覆盖参数:
echo 'set -- arg; cd /place/to/execute' | cat - test.sh | ssh -T user@hostname
以上将第一个参数设置为arg
。
一般:
set -- arg1 arg2 arg3
将覆盖 bash 中的 </code>、<code>
、</code>。</p>
<p>这基本上会使 <code>cat - test.sh
的结果成为一个不需要任何参数的独立脚本。
取决于您拥有的脚本的复杂程度。您可能希望重写它以便能够使用 rpcsh 功能从您的脚本远程执行 shell 函数。
使用 https://gist.github.com/Shadowfen/2b510e51da6915adedfb 保存到 /usr/local/include/rpcsh.inc(例如)你可以有一个脚本
#!/bin/sh
source /usr/local/include/rpcsh.inc
MASTER_ARG=""
function ahelper() {
# used by doremotely just to show that we can
echo "master arg 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
这将在包含
的远程主机上为您提供一个 ~/sample_rpcsh.txt 文件master arg newvalue was passed in
rpcsh.inc 的副本(以防 link 变坏):
#!/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" -- "$@")")
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 "" ]
do
case in
-h|--host) host=;
shift; shift;;
-u|--user) user=;
shift; shift;;
-v|--pushvars) pushvars="$pushvars ";
shift; shift;;
-f|--pushfuncs) pushfuncs="$pushfuncs ";
shift; shift;;
-r|--run) run=;
shift; shift;;
-A) sshvars=( "${sshvars[@]}" -A );
shift;;
-i) sshvars=( "${sshvars[@]}" -i );
shift; shift;;
--) shift; break;;
esac
done
remote_args=( "$@" )
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[@]}
"
}