Oneliner to run commands on multiple servers

sxh () { for i in "${@:2}"; do ssh "$i" "$1"; done ; }
Oneliner to run commands on multiple servers over ssh. - First parameter "$1" is the command you want to execute remotely. ( It can be multiple commands e.g. "hostname;uptime") - Second parameter "${@:2}" represents the remote host/s you want to run the command/s on.
Sample Output
$ sxh "hostname;uptime" Host1 Host2
 11:25am  up 7 days,  4:42,  0 users,  load average: 0.09, 0.08, 0.05
 11:25am  up 7 days,  4:34,  1 user,  load average: 0.00, 0.00, 0.01

By: zlemini
2013-04-12 11:34:02

What Others Think

I don't think I'll need your command, but you can have a +1 for the ${@:2} -- very useful :-)
flatcap · 480 weeks and 4 days ago
what is sxh? is this on the default install for RHEL?
andrewm659 · 476 weeks and 1 day ago
sxh is just the name i gave to the function. You can call it whatever you want, it should work on recent versions of RHEL.
zlemini · 437 weeks and 5 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: