【问题标题】:Executing a local script on a remote Machine在远程机器上执行本地脚本
【发布时间】:2016-06-28 07:54:41
【问题描述】:

我在我的本地机器上有一个脚本,但需要在远程机器上运行它而不把它复制到那里(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" 是结果错误

【问题讨论】:

    标签: bash shell ssh command-line-arguments


    【解决方案1】:

    您需要覆盖参数:

    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 的结果成为一个不需要任何参数的独立脚本。

    【讨论】:

      【解决方案2】:

      取决于您拥有的脚本的复杂性。您可能需要重写它,以便能够使用 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[@]}
         "
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-16
        • 2017-09-08
        • 1970-01-01
        • 2016-04-02
        • 1970-01-01
        • 2013-10-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多