Reputation: 2673
I want to run the same set of Unix commands on multiple machines. I am aware of ssh and something like the below. I want to write a shell script to do this. I have access to bash and ksh and I'm on Linux Red Hat 5.
ssh root@ip "echo \$HOME"
However, I have 2 questions:
Upvotes: 2
Views: 6318
Reputation: 824
Send a list of commands to the remote shell. Possible solutions:
use "
, escape line breaks to format code and end each substatement with ;
.
Disadvantage: "
should not be used inside the command list.
ssh user@ip "\
echo 'Hallo sir, how are you doing?';\
whoami;\
cd /;\
ls\
"
use '
and format code with regular line breaks.
Disadvantage: '
should not be used inside the command list.
ssh user@ip '
echo "Hallo sir, how are you doing?"
whoami
cd /
ls
'
Note: using "
or '
inside the respective statements will not necessarily result in an error. Though you may get unsuspected results.
Upvotes: 0
Reputation: 44623
You should use key based authentification, possibly coupled with ssh-agent
to remember key passphrase.
You can invoke sh -c
as the command, and pass it a string containing the list of command to execute.ssh
invoke a shell on the remote machine, so you can pass a list of command as a string.
For example:
$ ssh user@ip "echo 'Hello world'; whoami; cd / ; ls"
Upvotes: 7
Reputation: 1764
Use ssh-agent to set up authentication for all commands. Or put your multiple commands into a single shell script.
Upvotes: 1