Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to execute a script saved on the local system from a remote Unix server to help me automate a deployment process. The script contains some commands to rename an existing file on the remote Unix server and then transfer a new file from the local system to the Unix server.

Is there a way to do this?

share|improve this question
Something along the lines of cat | ssh remotehost 'cat - | sh', perhaps? I haven't tested that, but maybe it can get you started. It avoids having to copy the actual file or making the server aware of your local file system. – Michael Kjörling Mar 11 '13 at 10:34
up vote 0 down vote accepted

The tool of your choice is ssh.
Simply call ssh user@remoteHost remoteScript to trigger the invocation on the local system from the remote unix server. See man ssh for further details.

If the script is not already on the destination machine, you have two ways to achieve it.

via scp

function remoteCallSCP() { # <remoteAccount> <script> <interpreter> [arguments}       
   local remoteAccount=${1}
   local script=${2}
   shift scriptRunner=${3} 
   shift 3 # script arguments are following
   local tmpFile=$(mktemp) # not perfect, but 99% good
   scp ${script} ${remoteAccount}:${tmpFile} # executable bit is lost
   ssh ${remoteAccount} ${scriptRunner} ${tmpFile} 


via ssh and stdin

 function remoteCallStdIn() { # <remoteAccount> <script>
     local remoteAccount=${1}
     local script=${2}
     ssh -T ${remoteAccount}  <${script}

The -T option disable pseudo-tty allocation. Which may help to fix some weird issues.

The following features are missing here:

  • The script has to be a shell script compatible to the default shell of the remote account.
  • Command line arguments are not working.

As a maintainable workaround you may think about custom generated scripts as input, that are containing interpreter and arguments.

share|improve this answer
Thanks worked – Tapan Mar 11 '13 at 12:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .