Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 localscriptfile.sh | 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
add comment

1 Answer

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 Schimitt...it worked –  Tapan Mar 11 '13 at 12:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.