How can I run commands in batch mode over ssh? That is, what is the ssh command's equivalent of sftp -b <filename> <hostname>?

I have a set of commands which I wish to run across a set of hosts connecting over ssh. Over sftp, i store the commands in a file filename and connect to the host and run the commands using the previously mentioned command.

Is something like that possible over ssh?

  • I have gone through this question. But I still could not figure out how to run the commands in batch mode. – Srikanth Mar 25 '11 at 10:58
  • And can some one please create a tag #batchmode and tag this question to that? – Srikanth Mar 25 '11 at 11:01

Correct me if I'm wrong, but you seem to be wanting to run regular shell commands on the remote server where the script is local.

trap "rm -f /tmp/sendonssh.$$.*" 0 1 2 3 15
# commands to run on the remote server
cat <<'EOF' >> /tmp/sendonssh.$$.sh
mkdir -p /tmp/foobar.$$
mv $HOME/xyzzy /tmp/foobar.$$
chmod 640 $HOME/xyzzy
# call for each argument
for userhost in "$@"; do
    errorout=`ssh -aTxo BatchMode=yes $userhost /bin/sh -s < /tmp/sendonssh.$$.sh 2>&1`
    if [ $rc -ne 0 ]; then
        echo "Error: $userhost: $errorout"
        exit $rc

I do this with some 'remote execution' apps in my test environment using Python instead of the shell: ssh $userhost python < $pythonscriptfilename.

  • thanks for the answer. That looks like it. I'm tweaking it a bit and trying it out now. – Srikanth Mar 25 '11 at 11:31

The SSH equivalent of sftp -b <filename> <hostname> would be:

ssh -o BatchMode=yes <hostname> sh -s < "<filename>"


How about to keep it simple and run the "batch" file on the other computer?

  1. scp batch-file user@pc
  2. ssh user@pc batch-file
  3. ssh user@pc rm batch-file

And the batch file would be a normal shell script so the syntax is well known.


man expect? :\

but it's not the perfect way.

  • Hi johnny8888, I'm on a Solaris server and there is no manual entry for expect. Can you please elaborate your answer? – Srikanth Mar 25 '11 at 11:12
  • @Srikanth linux.die.net/man/1/expect – Michael Mrozek Mar 25 '11 at 13:08
  • @Srikanth: expect is designed to interact with programs that expect to be running on a terminal. This is unrelated to the problem of running commands over ssh. – Gilles 'SO- stop being evil' Mar 25 '11 at 20:01


ssh user@host sh -c "$(<filename)"

You could use ssh forced commands.

These are associated with a particular key. When an authentication is done with that key, that command is run and the connection exits. One advantage of this approach is increased security, since in that case the key can't be used to get to a login shell.


Another option to a script by Arcege would be a Bash function:

sshbatch() {
  # Expect at least 2 parameters, if less are provided print help
  if [[ ${#@} -lt 2 ]]; then
    printf 'Usage: sshbatch [user@]host... input_file\n'
    while read -r -u "$fd" host; do
      # Check if the last parameter is a readable file, else print error and exit
      [[ -r ${@:(-1)} ]] || { printf "The file ${@:(-1)} is not readable!\n"; break; }
      # Run remote bash from the file given in the last parameter
      ssh -o BatchMode=yes "$host" bash -s < "${@:(-1)}"
      # Read host list from 1st to next to last parameters
    done {fd}< <(printf '%s\n' "${@:1:${#@}-1}")

I don't use ssh's -T option, as it doesn't work with it in all cases.

This script uses parameter expansion on positional parameters array $@:

  • "${@:(-1)}" expands to the last parameter (literally first form the end onwards)
  • "${#@}" expands to the number of positional parameters
  • "${@:1:${#@}-1}" expands to a list of all parameters from first to next to last (literally number of parameters less one).
  • I'm not so good on the variable expansions like that, would you be able to add a little on what's going on? Cheers. – Guy Feb 2 '18 at 14:13
  • @Guy, here you are — I commented the code and added some explanation on parameter expansion – gadamiak Feb 5 '18 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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