2016-06-27 5 views
0

ローカルマシン上にスクリプトがありますが、リモートマシン上でスクリプトを実行する必要があります(IE、それ以上のスクリプトは実行できません)リモートマシンでローカルスクリプトを実行する

私は現在、しかし、私はまたtest.sh.にコマンドライン引数を提供する必要があり、次の機能のコマンド

echo 'cd /place/to/execute' | cat - test.sh | ssh -T [email protected] 

を持っています

私はちょうど私がローカル実行の場合と同様に、.SH後にそれを追加するが、それは動作しませんでした試みた:

echo 'cd /place/to/execute' | cat - test.sh "arg" | ssh -T [email protected] 

「猫:引数:そのようなファイルやディレクトリはありませんが、」結果のエラーです

答えて

3

あなたは引数を上書きする必要があります。

echo 'set -- arg; cd /place/to/execute' | cat - test.sh | ssh -T [email protected] 

は、上記argに最初の引数を設定します。一般

set -- arg1 arg2 arg3

はbashで$1$2$3が上書きされます。

これは、基本的にcat - test.shの結果を、引数を必要としないスタンドアロンスクリプトにします。

0

あなたが持っているスクリプトの複雑さによって異なります。 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[@]} 
    " 
} 
関連する問題