How to run the same command on multiple servers

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








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?










share|improve this question






























    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?










    share|improve this question


























      0












      0








      0


      1






      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?










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 8 at 17:18









      DopeGhoti

      46.8k56190




      46.8k56190










      asked Mar 8 at 16:02









      youngchawyoungchaw

      33




      33




















          2 Answers
          2






          active

          oldest

          votes


















          1














          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"





          share|improve this answer






























            1














            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):



            1. create list of hostnames like ~/.dev-hosts.list. This one will just have your list of hostnames


            dev-host1
            dev-host2
            ...



            1. Use following command to run date on each host from that list:

            pssh -h ~/.dev-hosts.list -l developer -i date




            1. Optional: wrap it all in an alias for convenience: alias run.on.dev='pssh -h ~/.dev-hosts.list -l username'. After that to run date 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.






            share|improve this answer























              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
              );



              );













              draft saved

              draft discarded


















              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









              1














              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"





              share|improve this answer



























                1














                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"





                share|improve this answer

























                  1












                  1








                  1







                  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"





                  share|improve this answer













                  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"






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 8 at 17:24









                  DopeGhotiDopeGhoti

                  46.8k56190




                  46.8k56190























                      1














                      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):



                      1. create list of hostnames like ~/.dev-hosts.list. This one will just have your list of hostnames


                      dev-host1
                      dev-host2
                      ...



                      1. Use following command to run date on each host from that list:

                      pssh -h ~/.dev-hosts.list -l developer -i date




                      1. Optional: wrap it all in an alias for convenience: alias run.on.dev='pssh -h ~/.dev-hosts.list -l username'. After that to run date 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.






                      share|improve this answer



























                        1














                        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):



                        1. create list of hostnames like ~/.dev-hosts.list. This one will just have your list of hostnames


                        dev-host1
                        dev-host2
                        ...



                        1. Use following command to run date on each host from that list:

                        pssh -h ~/.dev-hosts.list -l developer -i date




                        1. Optional: wrap it all in an alias for convenience: alias run.on.dev='pssh -h ~/.dev-hosts.list -l username'. After that to run date 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.






                        share|improve this answer

























                          1












                          1








                          1







                          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):



                          1. create list of hostnames like ~/.dev-hosts.list. This one will just have your list of hostnames


                          dev-host1
                          dev-host2
                          ...



                          1. Use following command to run date on each host from that list:

                          pssh -h ~/.dev-hosts.list -l developer -i date




                          1. Optional: wrap it all in an alias for convenience: alias run.on.dev='pssh -h ~/.dev-hosts.list -l username'. After that to run date 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.






                          share|improve this answer













                          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):



                          1. create list of hostnames like ~/.dev-hosts.list. This one will just have your list of hostnames


                          dev-host1
                          dev-host2
                          ...



                          1. Use following command to run date on each host from that list:

                          pssh -h ~/.dev-hosts.list -l developer -i date




                          1. Optional: wrap it all in an alias for convenience: alias run.on.dev='pssh -h ~/.dev-hosts.list -l username'. After that to run date 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.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 8 at 20:09









                          Alex FAlex F

                          112




                          112



























                              draft saved

                              draft discarded
















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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






                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay