How to run the same command on multiple servers
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have 4 suse server working as cluster. name CS-1 CS-2 CS-3 CS-4
On CS-1 can ssh to CS-2 CS-3 CS-4 directly (ssh CS-2, ssh CS-3, ssh CS-4)
On CS-2 can ssh to CS-1 CS-3 CS-4 directly (ssh CS-1, ssh CS-3, ssh CS-4)
I have 3 commands need to execute on all of these 4 servers
sed 's/aes128-ctr/aes129-ctr/' </etc/ssh/sshd_config> sshd_config.new; mv sshd_config.new sshd_config
sed 's/aes130-ctr/aes131-ctr/' </etc/ssh/sshd_config> sshd_config.new; mv sshd_config.new sshd_config
sed 's/aes132-ctr/aes133-ctr/' </etc/ssh/sshd_config> sshd_config.new; mv sshd_config.new sshd_config
how to do this in one shell script?
shell-script ssh
add a comment |
I have 4 suse server working as cluster. name CS-1 CS-2 CS-3 CS-4
On CS-1 can ssh to CS-2 CS-3 CS-4 directly (ssh CS-2, ssh CS-3, ssh CS-4)
On CS-2 can ssh to CS-1 CS-3 CS-4 directly (ssh CS-1, ssh CS-3, ssh CS-4)
I have 3 commands need to execute on all of these 4 servers
sed 's/aes128-ctr/aes129-ctr/' </etc/ssh/sshd_config> sshd_config.new; mv sshd_config.new sshd_config
sed 's/aes130-ctr/aes131-ctr/' </etc/ssh/sshd_config> sshd_config.new; mv sshd_config.new sshd_config
sed 's/aes132-ctr/aes133-ctr/' </etc/ssh/sshd_config> sshd_config.new; mv sshd_config.new sshd_config
how to do this in one shell script?
shell-script ssh
add a comment |
I have 4 suse server working as cluster. name CS-1 CS-2 CS-3 CS-4
On CS-1 can ssh to CS-2 CS-3 CS-4 directly (ssh CS-2, ssh CS-3, ssh CS-4)
On CS-2 can ssh to CS-1 CS-3 CS-4 directly (ssh CS-1, ssh CS-3, ssh CS-4)
I have 3 commands need to execute on all of these 4 servers
sed 's/aes128-ctr/aes129-ctr/' </etc/ssh/sshd_config> sshd_config.new; mv sshd_config.new sshd_config
sed 's/aes130-ctr/aes131-ctr/' </etc/ssh/sshd_config> sshd_config.new; mv sshd_config.new sshd_config
sed 's/aes132-ctr/aes133-ctr/' </etc/ssh/sshd_config> sshd_config.new; mv sshd_config.new sshd_config
how to do this in one shell script?
shell-script ssh
I have 4 suse server working as cluster. name CS-1 CS-2 CS-3 CS-4
On CS-1 can ssh to CS-2 CS-3 CS-4 directly (ssh CS-2, ssh CS-3, ssh CS-4)
On CS-2 can ssh to CS-1 CS-3 CS-4 directly (ssh CS-1, ssh CS-3, ssh CS-4)
I have 3 commands need to execute on all of these 4 servers
sed 's/aes128-ctr/aes129-ctr/' </etc/ssh/sshd_config> sshd_config.new; mv sshd_config.new sshd_config
sed 's/aes130-ctr/aes131-ctr/' </etc/ssh/sshd_config> sshd_config.new; mv sshd_config.new sshd_config
sed 's/aes132-ctr/aes133-ctr/' </etc/ssh/sshd_config> sshd_config.new; mv sshd_config.new sshd_config
how to do this in one shell script?
shell-script ssh
shell-script ssh
edited Mar 8 at 17:18
DopeGhoti
46.8k56190
46.8k56190
asked Mar 8 at 16:02
youngchawyoungchaw
33
33
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
There are several ways to do this sort of thing.
If this is the sort of thing you're likely to be doing a lot, you might want to look into Ansible or other similar automation tooling.
If this is a one-off, probably the most straightforward way is to copy those commands into a script, scp
the script to the remote machines, and then remotely execute them with ssh
:
$ for host in cs-2..4; do scp my-script.sh myuser@"$host":; ssh -t $host "sudo ./my-script.sh"; done
As an aside, if you are confident in your sed
command, rather than manually writing to a temp file and copying the new one in place, you can do something such as:
sed --in-place 's/aes128-ctr/aes129-ctr/;s/aes130-ctr/aes131-ctr/;s/aes132-ctr/aes133-ctr/' /etc/ssh/sshd_config
As this is one command, you can then simply for each host:
$ ssh user@host "sed --in-place 's/aes128-ctr/aes129-ctr/;s/aes130-ctr/aes131-ctr/;s/aes132-ctr/aes133-ctr/' /etc/ssh/sshd_config"
add a comment |
If you have to run same commands on some set of servers quite often, I would recommend to look into pssh
tool and related tools from the same package.
What I've done on my laptop, from where I need to control ~50 remote hosts (with ssh set up use key authentication already):
- create list of hostnames like
~/.dev-hosts.list
. This one will just have your list of hostnames
dev-host1
dev-host2
...
- Use following command to run
date
on each host from that list:
pssh -h ~/.dev-hosts.list -l developer -i date
Optional: wrap it all in an alias for convenience:alias run.on.dev='pssh -h ~/.dev-hosts.list -l username'
. After that to rundate
on these machines and get output you are just few keystrokes away:
run.on.dev -i date
Obviously, you can execute this way practically any command, including your sed
cases.
Note that -l
allows you to specify username to ssh to these hosts, and -i
allows you to see the output your command produces.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505159%2fhow-to-run-the-same-command-on-multiple-servers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are several ways to do this sort of thing.
If this is the sort of thing you're likely to be doing a lot, you might want to look into Ansible or other similar automation tooling.
If this is a one-off, probably the most straightforward way is to copy those commands into a script, scp
the script to the remote machines, and then remotely execute them with ssh
:
$ for host in cs-2..4; do scp my-script.sh myuser@"$host":; ssh -t $host "sudo ./my-script.sh"; done
As an aside, if you are confident in your sed
command, rather than manually writing to a temp file and copying the new one in place, you can do something such as:
sed --in-place 's/aes128-ctr/aes129-ctr/;s/aes130-ctr/aes131-ctr/;s/aes132-ctr/aes133-ctr/' /etc/ssh/sshd_config
As this is one command, you can then simply for each host:
$ ssh user@host "sed --in-place 's/aes128-ctr/aes129-ctr/;s/aes130-ctr/aes131-ctr/;s/aes132-ctr/aes133-ctr/' /etc/ssh/sshd_config"
add a comment |
There are several ways to do this sort of thing.
If this is the sort of thing you're likely to be doing a lot, you might want to look into Ansible or other similar automation tooling.
If this is a one-off, probably the most straightforward way is to copy those commands into a script, scp
the script to the remote machines, and then remotely execute them with ssh
:
$ for host in cs-2..4; do scp my-script.sh myuser@"$host":; ssh -t $host "sudo ./my-script.sh"; done
As an aside, if you are confident in your sed
command, rather than manually writing to a temp file and copying the new one in place, you can do something such as:
sed --in-place 's/aes128-ctr/aes129-ctr/;s/aes130-ctr/aes131-ctr/;s/aes132-ctr/aes133-ctr/' /etc/ssh/sshd_config
As this is one command, you can then simply for each host:
$ ssh user@host "sed --in-place 's/aes128-ctr/aes129-ctr/;s/aes130-ctr/aes131-ctr/;s/aes132-ctr/aes133-ctr/' /etc/ssh/sshd_config"
add a comment |
There are several ways to do this sort of thing.
If this is the sort of thing you're likely to be doing a lot, you might want to look into Ansible or other similar automation tooling.
If this is a one-off, probably the most straightforward way is to copy those commands into a script, scp
the script to the remote machines, and then remotely execute them with ssh
:
$ for host in cs-2..4; do scp my-script.sh myuser@"$host":; ssh -t $host "sudo ./my-script.sh"; done
As an aside, if you are confident in your sed
command, rather than manually writing to a temp file and copying the new one in place, you can do something such as:
sed --in-place 's/aes128-ctr/aes129-ctr/;s/aes130-ctr/aes131-ctr/;s/aes132-ctr/aes133-ctr/' /etc/ssh/sshd_config
As this is one command, you can then simply for each host:
$ ssh user@host "sed --in-place 's/aes128-ctr/aes129-ctr/;s/aes130-ctr/aes131-ctr/;s/aes132-ctr/aes133-ctr/' /etc/ssh/sshd_config"
There are several ways to do this sort of thing.
If this is the sort of thing you're likely to be doing a lot, you might want to look into Ansible or other similar automation tooling.
If this is a one-off, probably the most straightforward way is to copy those commands into a script, scp
the script to the remote machines, and then remotely execute them with ssh
:
$ for host in cs-2..4; do scp my-script.sh myuser@"$host":; ssh -t $host "sudo ./my-script.sh"; done
As an aside, if you are confident in your sed
command, rather than manually writing to a temp file and copying the new one in place, you can do something such as:
sed --in-place 's/aes128-ctr/aes129-ctr/;s/aes130-ctr/aes131-ctr/;s/aes132-ctr/aes133-ctr/' /etc/ssh/sshd_config
As this is one command, you can then simply for each host:
$ ssh user@host "sed --in-place 's/aes128-ctr/aes129-ctr/;s/aes130-ctr/aes131-ctr/;s/aes132-ctr/aes133-ctr/' /etc/ssh/sshd_config"
answered Mar 8 at 17:24
DopeGhotiDopeGhoti
46.8k56190
46.8k56190
add a comment |
add a comment |
If you have to run same commands on some set of servers quite often, I would recommend to look into pssh
tool and related tools from the same package.
What I've done on my laptop, from where I need to control ~50 remote hosts (with ssh set up use key authentication already):
- create list of hostnames like
~/.dev-hosts.list
. This one will just have your list of hostnames
dev-host1
dev-host2
...
- Use following command to run
date
on each host from that list:
pssh -h ~/.dev-hosts.list -l developer -i date
Optional: wrap it all in an alias for convenience:alias run.on.dev='pssh -h ~/.dev-hosts.list -l username'
. After that to rundate
on these machines and get output you are just few keystrokes away:
run.on.dev -i date
Obviously, you can execute this way practically any command, including your sed
cases.
Note that -l
allows you to specify username to ssh to these hosts, and -i
allows you to see the output your command produces.
add a comment |
If you have to run same commands on some set of servers quite often, I would recommend to look into pssh
tool and related tools from the same package.
What I've done on my laptop, from where I need to control ~50 remote hosts (with ssh set up use key authentication already):
- create list of hostnames like
~/.dev-hosts.list
. This one will just have your list of hostnames
dev-host1
dev-host2
...
- Use following command to run
date
on each host from that list:
pssh -h ~/.dev-hosts.list -l developer -i date
Optional: wrap it all in an alias for convenience:alias run.on.dev='pssh -h ~/.dev-hosts.list -l username'
. After that to rundate
on these machines and get output you are just few keystrokes away:
run.on.dev -i date
Obviously, you can execute this way practically any command, including your sed
cases.
Note that -l
allows you to specify username to ssh to these hosts, and -i
allows you to see the output your command produces.
add a comment |
If you have to run same commands on some set of servers quite often, I would recommend to look into pssh
tool and related tools from the same package.
What I've done on my laptop, from where I need to control ~50 remote hosts (with ssh set up use key authentication already):
- create list of hostnames like
~/.dev-hosts.list
. This one will just have your list of hostnames
dev-host1
dev-host2
...
- Use following command to run
date
on each host from that list:
pssh -h ~/.dev-hosts.list -l developer -i date
Optional: wrap it all in an alias for convenience:alias run.on.dev='pssh -h ~/.dev-hosts.list -l username'
. After that to rundate
on these machines and get output you are just few keystrokes away:
run.on.dev -i date
Obviously, you can execute this way practically any command, including your sed
cases.
Note that -l
allows you to specify username to ssh to these hosts, and -i
allows you to see the output your command produces.
If you have to run same commands on some set of servers quite often, I would recommend to look into pssh
tool and related tools from the same package.
What I've done on my laptop, from where I need to control ~50 remote hosts (with ssh set up use key authentication already):
- create list of hostnames like
~/.dev-hosts.list
. This one will just have your list of hostnames
dev-host1
dev-host2
...
- Use following command to run
date
on each host from that list:
pssh -h ~/.dev-hosts.list -l developer -i date
Optional: wrap it all in an alias for convenience:alias run.on.dev='pssh -h ~/.dev-hosts.list -l username'
. After that to rundate
on these machines and get output you are just few keystrokes away:
run.on.dev -i date
Obviously, you can execute this way practically any command, including your sed
cases.
Note that -l
allows you to specify username to ssh to these hosts, and -i
allows you to see the output your command produces.
answered Mar 8 at 20:09
Alex FAlex F
112
112
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505159%2fhow-to-run-the-same-command-on-multiple-servers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown