2016-06-27 17 views
0

Ich habe ein Skript auf meinem lokalen Rechner, muss es aber auf einem Remote-Rechner ausführen, ohne es dort zu kopieren (IE, ich kann es nicht sftp und einfach dort ausführen)Ausführen eines lokalen Skripts auf einem Remote-Rechner

Im Moment habe ich den folgenden Befehl funktioniert

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

Allerdings muss ich auch ein Kommandozeilen-Argument liefern test.sh.

Ich habe gerade versucht es nach dem .sh Zugabe, wie würde ich für die lokale Ausführung, aber das hat nicht funktioniert:

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

„Katze: arg: Keine solche Datei oder das Verzeichnis“ ist der resultierende Fehler

Antwort

3

Sie müssen die Argumente außer Kraft zu setzen:

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

die oben wird das erste Argument arg gesetzt.

Allgemein:

set -- arg1 arg2 arg3

überschreibt die $1, $2, $3 in bash.

Dies wird im Grunde das Ergebnis cat - test.sh ein eigenständiges Skript, das keine Argumente benötigt ".

0

Hängt von der Komplexität des Skripts ab, das Sie haben. Vielleicht möchten Sie es neu schreiben, um mit der rpcsh-Funktionalität Shell-Funktionen aus Ihrem Skript ausführen zu können.

https://gist.github.com/Shadowfen/2b510e51da6915adedfb Verwendung in /usr/local/include/rpcsh.inc gespeichert (zum Beispiel) ein Skript

#!/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 
haben könnte

Dies gibt Ihnen eine ~/sample_rpcsh.txt Datei auf dem Remote-Host, enthält

master arg newvalue was passed in 

Kopie des rpcsh.inc (bei Link geht schlecht):

#!/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[@]} 
    " 
} 
Verwandte Themen