在远程计算机上执行本地脚本

时间:2016-06-27 19:21:31

标签: bash shell ssh command-line-arguments

我的本​​地计算机上有一个脚本,但是需要在远程计算机上运行它而不将其复制到那里(IE,我不能将其翻过来并在那里运行)

我目前有以下功能命令

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:没有这样的文件或目录"是结果错误

2 个答案:

答案 0 :(得分:3)

您需要覆盖参数:

echo 'set -- arg; cd /place/to/execute' | cat - test.sh | ssh -T user@hostname

以上将第一个参数设置为arg

一般而言:

set -- arg1 arg2 arg3

将覆盖bash中的$1$2$3

这基本上会使cat - test.sh的结果成为一个不需要任何参数的独立脚本。

答案 1 :(得分:0)

取决于您拥有的脚本的复杂性。您可能希望重写它以便能够使用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 $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

这将在包含

的远程主机上提供〜/ sample_rpcsh.txt文件
master arg newvalue was passed in

rpcsh.inc的副本(如果链接变坏):

#!/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 "$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=( "$@" )

    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[@]}
   "
}
相关问题