Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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

6 Answers 6

up vote 6 down vote accepted

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.

#!/bin/sh
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
EOF
# call for each argument
for userhost in "$@"; do
    errorout=`ssh -aTxo BatchMode=yes $userhost /bin/sh -s < /tmp/sendonssh.$$.sh 2>&1`
    rc=$?
    if [ $rc -ne 0 ]; then
        echo "Error: $userhost: $errorout"
        exit $rc
    fi
done

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

share|improve this answer
    
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>"

share|improve this answer

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.

share|improve this answer

man expect? :\

but it's not the perfect way.

share|improve this answer
    
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 Mar 25 '11 at 20:01

perhaps

ssh user@host sh -c "$(<filename)"
share|improve this answer

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.

share|improve this answer

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.