I need to loop through and ssh to multiple hosts and run a series of commands on each.
something like:
for i in $(jot -w '%02.0f' 14 1)
>do ssh user@host$i sudo -i "command1; command2; command3"
>done
but I can't get it to work correctly. I've seen various things on The Google like sudo sh -c
, piping to sudo, etc, but can't figure it out.
- I'm ssh'ing as a regular user that can sudo with no password (ssh as root not enabled)
- command1 not returning 0 should not prevent command2 from running, etc, hence
;
- I'm running the loop from a mac, hence
jot -w
, which is roughly equivalent toseq -f
in linux - ssh'ing to CentOS 5.4
- I'd like it to run with root's $PATH so I don't have to specify the full path to the commands, but it's not totally necessary
thanks in advance!
Trying to pass quoted command strings over ssh is always a challenge. You don't mention what sort of error messages you're getting, but here are a few thoughts:
The default sudo configuration on many systems requires that your session have a valid TTY associated with it. This requires either (a) calling ssh with the "-t" option, or (b) remove the "requiretty" option from your sudo configuration.
This works for me (the destination is a set of RHEL5 systems):
This results in:
There are a variety of tools that will make your life easier if you do this sort of thing often:
And so forth. I usually just use shell scripts, myself.
I find that getting the quoting correct is pretty annoying. Instead I tend to just pass commands to bash on the remote host through a pipe instead. This way you don't have to worry about getting the escaping right in your ssh command line. Just pass into the pipe exactly what you would type if you were connected interactively.
Which returns
I think pssh is what you need to work with.
http://sourceforge.net/projects/clusterssh/
This is a clear winner :D
Have a look at BatchLogin:
http://batchlogin.sourceforge.net/