Run Local Script On Remote System via SSH

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gabriel9999
    New Member
    • Jul 2019
    • 15

    Run Local Script On Remote System via SSH

    I have a script in my system. I want to run this script on the remote system which has SSH connection.
  • edizgeorgi
    New Member
    • Oct 2019
    • 8

    #2
    It is very easy just put the script file path after the ssh command like below.
    ssh ismail@192.168. 142.144 'bash -s' < cat myscript.sh
    Last edited by Rabbit; May 8 '20, 03:34 PM. Reason: External link removed per forum policy

    Comment

    • lewish95
      New Member
      • Mar 2020
      • 33

      #3
      You were pretty close with your example. It works just fine when you use it with arguments such as these.

      Sample script:

      $ more ex.bash
      #!/bin/bash

      echo $1 $2
      Example that works:

      $ ssh serverA "bash -s" < ./ex.bash "hi" "bye"
      hi bye
      But it fails for these types of arguments:

      $ ssh serverA "bash -s" < ./ex.bash "--time" "bye"
      bash: --: invalid option
      ...
      What's going on?
      The problem you're encountering is that the argument, -time, or --time in my example, is being interpreted as a switch to bash -s. You can pacify bash by terminating it from taking any of the remaining command line arguments for itself using the -- argument.

      Like this:

      $ ssh root@remoteServ er "bash -s" -- < /var/www/html/ops1/sysMole -time Aug 18 18
      Examples
      #1:

      $ ssh serverA "bash -s" -- < ./ex.bash "-time" "bye"
      -time bye
      #2:

      $ ssh serverA "bash -s" -- < ./ex.bash "--time" "bye"
      --time bye
      #3:

      $ ssh serverA "bash -s" -- < ./ex.bash --time "bye"
      --time bye
      #4:

      $ ssh < ./ex.bash serverA "bash -s -- --time bye"
      --time bye
      NOTE: Just to make it clear that wherever the redirection appears on the command line makes no difference, because ssh calls a remote shell with the concatenation of its arguments anyway, quoting doesn't make much difference, except when you need quoting on the remote shell like in example #4:

      $ ssh < ./ex.bash serverA "bash -s -- '<--time bye>' '<end>'"
      <--time bye> <end>

      Comment

      Working...