Run scripts remotely via SSHHow to remotely run a (shebang prefixed) node script using ssh?Get the source directory of a Bash script from within the script itselfHow do I prompt for Yes/No/Cancel input in a Linux shell script?How to use SSH to run a shell script on a remote machine?How to check if a program exists from a Bash script?Pipe to/from the clipboard in Bash scriptHow to specify the private SSH-key to use when executing shell command on Git?Check existence of input argument in a Bash shell scriptssh “permissions are too open” errorHow to download a file from server using SSH?ssh remote host identification has changed

Can birds evolve without trees?

How long should I wait to plug in my refrigerator after unplugging it?

Export economy of Mars

Is Norway in the Single Market?

Overprovisioning SSD on ubuntu. How? Ubuntu 19.04 Samsung SSD 860

Transistor design with beta variation

What's the proper way of indicating that a car has reached its destination during a dialogue?

If I buy and download a game through second Nintendo account do I own it on my main account too?

Declaring a visitor to the UK as my "girlfriend" - effect on getting a Visitor visa?

How do I respond appropriately to an overseas company that obtained a visa for me without hiring me?

What sort of solar system / atmospheric conditions, if any, would allow for a very cold planet that still receives plenty of light from its sun?

How to get maximum number that newcount can hold?

Return last number in sub-sequences in a list of integers

Does the problem of P vs NP come under the category of Operational Research?

Is the EU really banning "toxic propellants" in 2020? How is that going to work?

Define tcolorbox in math mode

Who's behind community AMIs on Amazon EC2?

Applied Meditation

cannot trash malware NGPlayerSetup.dmg

Why interlaced CRT scanning wasn't done back and forth?

Adding a (stair/baby) gate without facing walls

Is Illustrator accurate for business card sizes?

Reasons for using monsters as bioweapons

Can I say "Gesundheit" if someone is coughing?



Run scripts remotely via SSH


How to remotely run a (shebang prefixed) node script using ssh?Get the source directory of a Bash script from within the script itselfHow do I prompt for Yes/No/Cancel input in a Linux shell script?How to use SSH to run a shell script on a remote machine?How to check if a program exists from a Bash script?Pipe to/from the clipboard in Bash scriptHow to specify the private SSH-key to use when executing shell command on Git?Check existence of input argument in a Bash shell scriptssh “permissions are too open” errorHow to download a file from server using SSH?ssh remote host identification has changed






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








3















I need to collect user information from 100 remote servers. We have public/private key infrastructure for authentication, and I have configured ssh-agent command to forward key, meaning i can login on any server without password prompt (auto login).



Now I want to run a script on all server to collect user information (how many user account we have on all servers).



This is my script to collect user info.



#!/bin/bash
_l="/etc/login.defs"
_p="/etc/passwd"

## get mini UID limit ##
l=$(grep "^UID_MIN" $_l)

## get max UID limit ##
l1=$(grep "^UID_MAX" $_l)

awk -F':' -v "min=$l##UID_MIN" -v "max=$l1##UID_MAX" ' if ( $3 >= min && $3 <= max && $7 != "/sbin/nologin" ) print $0 ' "$_p"


I don't know how to run this script using ssh without interaction??










share|improve this question
































    3















    I need to collect user information from 100 remote servers. We have public/private key infrastructure for authentication, and I have configured ssh-agent command to forward key, meaning i can login on any server without password prompt (auto login).



    Now I want to run a script on all server to collect user information (how many user account we have on all servers).



    This is my script to collect user info.



    #!/bin/bash
    _l="/etc/login.defs"
    _p="/etc/passwd"

    ## get mini UID limit ##
    l=$(grep "^UID_MIN" $_l)

    ## get max UID limit ##
    l1=$(grep "^UID_MAX" $_l)

    awk -F':' -v "min=$l##UID_MIN" -v "max=$l1##UID_MAX" ' if ( $3 >= min && $3 <= max && $7 != "/sbin/nologin" ) print $0 ' "$_p"


    I don't know how to run this script using ssh without interaction??










    share|improve this question




























      3












      3








      3


      1






      I need to collect user information from 100 remote servers. We have public/private key infrastructure for authentication, and I have configured ssh-agent command to forward key, meaning i can login on any server without password prompt (auto login).



      Now I want to run a script on all server to collect user information (how many user account we have on all servers).



      This is my script to collect user info.



      #!/bin/bash
      _l="/etc/login.defs"
      _p="/etc/passwd"

      ## get mini UID limit ##
      l=$(grep "^UID_MIN" $_l)

      ## get max UID limit ##
      l1=$(grep "^UID_MAX" $_l)

      awk -F':' -v "min=$l##UID_MIN" -v "max=$l1##UID_MAX" ' if ( $3 >= min && $3 <= max && $7 != "/sbin/nologin" ) print $0 ' "$_p"


      I don't know how to run this script using ssh without interaction??










      share|improve this question
















      I need to collect user information from 100 remote servers. We have public/private key infrastructure for authentication, and I have configured ssh-agent command to forward key, meaning i can login on any server without password prompt (auto login).



      Now I want to run a script on all server to collect user information (how many user account we have on all servers).



      This is my script to collect user info.



      #!/bin/bash
      _l="/etc/login.defs"
      _p="/etc/passwd"

      ## get mini UID limit ##
      l=$(grep "^UID_MIN" $_l)

      ## get max UID limit ##
      l1=$(grep "^UID_MAX" $_l)

      awk -F':' -v "min=$l##UID_MIN" -v "max=$l1##UID_MAX" ' if ( $3 >= min && $3 <= max && $7 != "/sbin/nologin" ) print $0 ' "$_p"


      I don't know how to run this script using ssh without interaction??







      linux bash ssh autologin






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 20 '12 at 6:53









      doubleDown

      5,9211 gold badge19 silver badges41 bronze badges




      5,9211 gold badge19 silver badges41 bronze badges










      asked Oct 17 '12 at 15:17









      SatishSatish

      8,09321 gold badges71 silver badges119 bronze badges




      8,09321 gold badges71 silver badges119 bronze badges

























          6 Answers
          6






          active

          oldest

          votes


















          3














          Since you need to log into the remote machine there is AFAICT no way to do this "without ssh". However, ssh accepts a command to execute on the remote machine once logged in (instead of the shell it would start). So if you can save your script on the remote machine, e.g. as ~/script.sh, you can execute it without starting an interactive shell with



          $ ssh remote_machine ~/script.sh


          Once the script terminates the connection will automatically be closed (if you didn't configure that away purposely).






          share|improve this answer



























          • Ah! how did i miss scp :( I can do scp without password and push this script to /tmp directory of all remote server and run ssh to execute them.. thats for remind me.

            – Satish
            Oct 17 '12 at 15:32












          • If you're going to install things on each system anyway, why not go all the way and use a full-blown tool like Munin? You can add custom information to that, graph your results over time, set up notifications if hosts become unreachable. Monitoring is good. :-) There are instructions on installing Munin on Linux systems using yum within the Munin documentation.

            – ghoti
            Oct 17 '12 at 15:55


















          3














          Sounds like something you can do using expect.



          http://linux.die.net/man/1/expect




          Expect is a program that "talks" to other interactive programs according to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be.







          share|improve this answer

























          • I know we can use EXPECT prog. but i thought if i can find easy way to make it simple.

            – Satish
            Oct 17 '12 at 15:30











          • I am giving you +vote UP :)

            – Satish
            Oct 17 '12 at 15:33






          • 1





            Cool. I know I have some Expect scripts I used for getting information out of multiple servers... As soon as I find them, I'll publish a sample here.

            – andrux
            Oct 17 '12 at 15:36


















          2














          If you've got a key on each machine and can ssh remotehost from your monitoring host, you've got all that's required to collect the information you've asked for.



          #!/bin/bash

          servers=(wopr gerty mother)

          fmt="%st%st%sn"
          printf "$fmt" "Host" "UIDs" "Highest"
          printf "$fmt" "----" "----" "-------"

          count='awk "END print NR" /etc/passwd' # avoids whitespace problems from `wc`
          highest="awk -F: '$3>n&&$3<60000n=$3 ENDprint n' /etc/passwd"

          for server in $servers[@]; do
          printf "$fmt" "$server" "$(ssh "$server" "$count")" "$(ssh "$server" "$highest")"
          done


          Results for me:



          $ ./doit.sh
          Host UIDs Highest
          ---- ---- -------
          wopr 40 2020
          gerty 37 9001
          mother 32 534


          Note that this makes TWO ssh connections to each server to collect each datum. If you'd like to do this a little more efficiently, you can bundle the information into a single, slightly more complex collection script:



          #!/usr/local/bin/bash

          servers=(wopr gerty mother)

          fmt="%st%st%sn"
          printf "$fmt" "Host" "UIDs" "Highest"
          printf "$fmt" "----" "----" "-------"

          gather="awk -F: '$3>n&&$3<60000n=$3 ENDprint NR,n' /etc/passwd"

          for server in $servers[@]; do
          read count highest < <(ssh "$server" "$gather")
          printf "$fmt" "$server" "$count" "$highest"
          done


          (Identical results.)






          share|improve this answer
































            2














            ssh remoteserver.example /bin/bash < localscript.bash






            share|improve this answer
































              0














              (Note: the "proper" way to authenticate without manually entering in password is to use SSH keys. Storing password in plaintext even in your local scripts is a potential security vulnerability)



              You can run expect as part of your bash script. Here's a quick example that you can hack into your existing script:



              login=user
              IP=127.0.0.1
              password='your_password'

              expect_sh=$(expect -c "
              spawn ssh $login@$IP
              expect "password:"
              send "$passwordr"
              expect "#"
              send "./$remote_side_scriptr"
              expect "#"
              send "cd /libr"
              expect "#"
              send "cat file_namer"
              expect "#"
              send "exitr"
              ")

              echo "$expect_sh"


              You can also use pscp to copy files back and forth as part of a script so you don't need to manually supply the password as part of the interaction:



              Install putty-tools:



              $ sudo apt-get install putty-tools


              Using pscp in your script:



              pscp -scp -pw $password file_to_copy $login@$IP:$dest_dir





              share|improve this answer

























              • unfortunately i have RedHat system (yum). Can't compile or install third-party tools :(

                – Satish
                Oct 17 '12 at 15:36


















              0














              maybe you'd like to try the expect command as following



              #!/usr/bin/expect
              set timeout 30
              spawn ssh -p ssh_port -l ssh_username ssh_server_host
              expect "password:"
              send "your_passwdr"
              interact


              the expect command will catch the "password:" and then auto fill the passwd your send by above.



              Remember that replace the ssh_port, ssh_username, ssh_server_host and your_passwd with your own configure






              share|improve this answer



























                Your Answer






                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                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: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                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%2fstackoverflow.com%2fquestions%2f12937634%2frun-scripts-remotely-via-ssh%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                6 Answers
                6






                active

                oldest

                votes








                6 Answers
                6






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                3














                Since you need to log into the remote machine there is AFAICT no way to do this "without ssh". However, ssh accepts a command to execute on the remote machine once logged in (instead of the shell it would start). So if you can save your script on the remote machine, e.g. as ~/script.sh, you can execute it without starting an interactive shell with



                $ ssh remote_machine ~/script.sh


                Once the script terminates the connection will automatically be closed (if you didn't configure that away purposely).






                share|improve this answer



























                • Ah! how did i miss scp :( I can do scp without password and push this script to /tmp directory of all remote server and run ssh to execute them.. thats for remind me.

                  – Satish
                  Oct 17 '12 at 15:32












                • If you're going to install things on each system anyway, why not go all the way and use a full-blown tool like Munin? You can add custom information to that, graph your results over time, set up notifications if hosts become unreachable. Monitoring is good. :-) There are instructions on installing Munin on Linux systems using yum within the Munin documentation.

                  – ghoti
                  Oct 17 '12 at 15:55















                3














                Since you need to log into the remote machine there is AFAICT no way to do this "without ssh". However, ssh accepts a command to execute on the remote machine once logged in (instead of the shell it would start). So if you can save your script on the remote machine, e.g. as ~/script.sh, you can execute it without starting an interactive shell with



                $ ssh remote_machine ~/script.sh


                Once the script terminates the connection will automatically be closed (if you didn't configure that away purposely).






                share|improve this answer



























                • Ah! how did i miss scp :( I can do scp without password and push this script to /tmp directory of all remote server and run ssh to execute them.. thats for remind me.

                  – Satish
                  Oct 17 '12 at 15:32












                • If you're going to install things on each system anyway, why not go all the way and use a full-blown tool like Munin? You can add custom information to that, graph your results over time, set up notifications if hosts become unreachable. Monitoring is good. :-) There are instructions on installing Munin on Linux systems using yum within the Munin documentation.

                  – ghoti
                  Oct 17 '12 at 15:55













                3












                3








                3







                Since you need to log into the remote machine there is AFAICT no way to do this "without ssh". However, ssh accepts a command to execute on the remote machine once logged in (instead of the shell it would start). So if you can save your script on the remote machine, e.g. as ~/script.sh, you can execute it without starting an interactive shell with



                $ ssh remote_machine ~/script.sh


                Once the script terminates the connection will automatically be closed (if you didn't configure that away purposely).






                share|improve this answer















                Since you need to log into the remote machine there is AFAICT no way to do this "without ssh". However, ssh accepts a command to execute on the remote machine once logged in (instead of the shell it would start). So if you can save your script on the remote machine, e.g. as ~/script.sh, you can execute it without starting an interactive shell with



                $ ssh remote_machine ~/script.sh


                Once the script terminates the connection will automatically be closed (if you didn't configure that away purposely).







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 27 at 0:36









                Ricardo

                1,7031 gold badge18 silver badges35 bronze badges




                1,7031 gold badge18 silver badges35 bronze badges










                answered Oct 17 '12 at 15:23









                Benjamin BannierBenjamin Bannier

                36.8k9 gold badges47 silver badges75 bronze badges




                36.8k9 gold badges47 silver badges75 bronze badges















                • Ah! how did i miss scp :( I can do scp without password and push this script to /tmp directory of all remote server and run ssh to execute them.. thats for remind me.

                  – Satish
                  Oct 17 '12 at 15:32












                • If you're going to install things on each system anyway, why not go all the way and use a full-blown tool like Munin? You can add custom information to that, graph your results over time, set up notifications if hosts become unreachable. Monitoring is good. :-) There are instructions on installing Munin on Linux systems using yum within the Munin documentation.

                  – ghoti
                  Oct 17 '12 at 15:55

















                • Ah! how did i miss scp :( I can do scp without password and push this script to /tmp directory of all remote server and run ssh to execute them.. thats for remind me.

                  – Satish
                  Oct 17 '12 at 15:32












                • If you're going to install things on each system anyway, why not go all the way and use a full-blown tool like Munin? You can add custom information to that, graph your results over time, set up notifications if hosts become unreachable. Monitoring is good. :-) There are instructions on installing Munin on Linux systems using yum within the Munin documentation.

                  – ghoti
                  Oct 17 '12 at 15:55
















                Ah! how did i miss scp :( I can do scp without password and push this script to /tmp directory of all remote server and run ssh to execute them.. thats for remind me.

                – Satish
                Oct 17 '12 at 15:32






                Ah! how did i miss scp :( I can do scp without password and push this script to /tmp directory of all remote server and run ssh to execute them.. thats for remind me.

                – Satish
                Oct 17 '12 at 15:32














                If you're going to install things on each system anyway, why not go all the way and use a full-blown tool like Munin? You can add custom information to that, graph your results over time, set up notifications if hosts become unreachable. Monitoring is good. :-) There are instructions on installing Munin on Linux systems using yum within the Munin documentation.

                – ghoti
                Oct 17 '12 at 15:55





                If you're going to install things on each system anyway, why not go all the way and use a full-blown tool like Munin? You can add custom information to that, graph your results over time, set up notifications if hosts become unreachable. Monitoring is good. :-) There are instructions on installing Munin on Linux systems using yum within the Munin documentation.

                – ghoti
                Oct 17 '12 at 15:55













                3














                Sounds like something you can do using expect.



                http://linux.die.net/man/1/expect




                Expect is a program that "talks" to other interactive programs according to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be.







                share|improve this answer

























                • I know we can use EXPECT prog. but i thought if i can find easy way to make it simple.

                  – Satish
                  Oct 17 '12 at 15:30











                • I am giving you +vote UP :)

                  – Satish
                  Oct 17 '12 at 15:33






                • 1





                  Cool. I know I have some Expect scripts I used for getting information out of multiple servers... As soon as I find them, I'll publish a sample here.

                  – andrux
                  Oct 17 '12 at 15:36















                3














                Sounds like something you can do using expect.



                http://linux.die.net/man/1/expect




                Expect is a program that "talks" to other interactive programs according to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be.







                share|improve this answer

























                • I know we can use EXPECT prog. but i thought if i can find easy way to make it simple.

                  – Satish
                  Oct 17 '12 at 15:30











                • I am giving you +vote UP :)

                  – Satish
                  Oct 17 '12 at 15:33






                • 1





                  Cool. I know I have some Expect scripts I used for getting information out of multiple servers... As soon as I find them, I'll publish a sample here.

                  – andrux
                  Oct 17 '12 at 15:36













                3












                3








                3







                Sounds like something you can do using expect.



                http://linux.die.net/man/1/expect




                Expect is a program that "talks" to other interactive programs according to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be.







                share|improve this answer













                Sounds like something you can do using expect.



                http://linux.die.net/man/1/expect




                Expect is a program that "talks" to other interactive programs according to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be.








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 17 '12 at 15:20









                andruxandrux

                1,4452 gold badges14 silver badges25 bronze badges




                1,4452 gold badges14 silver badges25 bronze badges















                • I know we can use EXPECT prog. but i thought if i can find easy way to make it simple.

                  – Satish
                  Oct 17 '12 at 15:30











                • I am giving you +vote UP :)

                  – Satish
                  Oct 17 '12 at 15:33






                • 1





                  Cool. I know I have some Expect scripts I used for getting information out of multiple servers... As soon as I find them, I'll publish a sample here.

                  – andrux
                  Oct 17 '12 at 15:36

















                • I know we can use EXPECT prog. but i thought if i can find easy way to make it simple.

                  – Satish
                  Oct 17 '12 at 15:30











                • I am giving you +vote UP :)

                  – Satish
                  Oct 17 '12 at 15:33






                • 1





                  Cool. I know I have some Expect scripts I used for getting information out of multiple servers... As soon as I find them, I'll publish a sample here.

                  – andrux
                  Oct 17 '12 at 15:36
















                I know we can use EXPECT prog. but i thought if i can find easy way to make it simple.

                – Satish
                Oct 17 '12 at 15:30





                I know we can use EXPECT prog. but i thought if i can find easy way to make it simple.

                – Satish
                Oct 17 '12 at 15:30













                I am giving you +vote UP :)

                – Satish
                Oct 17 '12 at 15:33





                I am giving you +vote UP :)

                – Satish
                Oct 17 '12 at 15:33




                1




                1





                Cool. I know I have some Expect scripts I used for getting information out of multiple servers... As soon as I find them, I'll publish a sample here.

                – andrux
                Oct 17 '12 at 15:36





                Cool. I know I have some Expect scripts I used for getting information out of multiple servers... As soon as I find them, I'll publish a sample here.

                – andrux
                Oct 17 '12 at 15:36











                2














                If you've got a key on each machine and can ssh remotehost from your monitoring host, you've got all that's required to collect the information you've asked for.



                #!/bin/bash

                servers=(wopr gerty mother)

                fmt="%st%st%sn"
                printf "$fmt" "Host" "UIDs" "Highest"
                printf "$fmt" "----" "----" "-------"

                count='awk "END print NR" /etc/passwd' # avoids whitespace problems from `wc`
                highest="awk -F: '$3>n&&$3<60000n=$3 ENDprint n' /etc/passwd"

                for server in $servers[@]; do
                printf "$fmt" "$server" "$(ssh "$server" "$count")" "$(ssh "$server" "$highest")"
                done


                Results for me:



                $ ./doit.sh
                Host UIDs Highest
                ---- ---- -------
                wopr 40 2020
                gerty 37 9001
                mother 32 534


                Note that this makes TWO ssh connections to each server to collect each datum. If you'd like to do this a little more efficiently, you can bundle the information into a single, slightly more complex collection script:



                #!/usr/local/bin/bash

                servers=(wopr gerty mother)

                fmt="%st%st%sn"
                printf "$fmt" "Host" "UIDs" "Highest"
                printf "$fmt" "----" "----" "-------"

                gather="awk -F: '$3>n&&$3<60000n=$3 ENDprint NR,n' /etc/passwd"

                for server in $servers[@]; do
                read count highest < <(ssh "$server" "$gather")
                printf "$fmt" "$server" "$count" "$highest"
                done


                (Identical results.)






                share|improve this answer





























                  2














                  If you've got a key on each machine and can ssh remotehost from your monitoring host, you've got all that's required to collect the information you've asked for.



                  #!/bin/bash

                  servers=(wopr gerty mother)

                  fmt="%st%st%sn"
                  printf "$fmt" "Host" "UIDs" "Highest"
                  printf "$fmt" "----" "----" "-------"

                  count='awk "END print NR" /etc/passwd' # avoids whitespace problems from `wc`
                  highest="awk -F: '$3>n&&$3<60000n=$3 ENDprint n' /etc/passwd"

                  for server in $servers[@]; do
                  printf "$fmt" "$server" "$(ssh "$server" "$count")" "$(ssh "$server" "$highest")"
                  done


                  Results for me:



                  $ ./doit.sh
                  Host UIDs Highest
                  ---- ---- -------
                  wopr 40 2020
                  gerty 37 9001
                  mother 32 534


                  Note that this makes TWO ssh connections to each server to collect each datum. If you'd like to do this a little more efficiently, you can bundle the information into a single, slightly more complex collection script:



                  #!/usr/local/bin/bash

                  servers=(wopr gerty mother)

                  fmt="%st%st%sn"
                  printf "$fmt" "Host" "UIDs" "Highest"
                  printf "$fmt" "----" "----" "-------"

                  gather="awk -F: '$3>n&&$3<60000n=$3 ENDprint NR,n' /etc/passwd"

                  for server in $servers[@]; do
                  read count highest < <(ssh "$server" "$gather")
                  printf "$fmt" "$server" "$count" "$highest"
                  done


                  (Identical results.)






                  share|improve this answer



























                    2












                    2








                    2







                    If you've got a key on each machine and can ssh remotehost from your monitoring host, you've got all that's required to collect the information you've asked for.



                    #!/bin/bash

                    servers=(wopr gerty mother)

                    fmt="%st%st%sn"
                    printf "$fmt" "Host" "UIDs" "Highest"
                    printf "$fmt" "----" "----" "-------"

                    count='awk "END print NR" /etc/passwd' # avoids whitespace problems from `wc`
                    highest="awk -F: '$3>n&&$3<60000n=$3 ENDprint n' /etc/passwd"

                    for server in $servers[@]; do
                    printf "$fmt" "$server" "$(ssh "$server" "$count")" "$(ssh "$server" "$highest")"
                    done


                    Results for me:



                    $ ./doit.sh
                    Host UIDs Highest
                    ---- ---- -------
                    wopr 40 2020
                    gerty 37 9001
                    mother 32 534


                    Note that this makes TWO ssh connections to each server to collect each datum. If you'd like to do this a little more efficiently, you can bundle the information into a single, slightly more complex collection script:



                    #!/usr/local/bin/bash

                    servers=(wopr gerty mother)

                    fmt="%st%st%sn"
                    printf "$fmt" "Host" "UIDs" "Highest"
                    printf "$fmt" "----" "----" "-------"

                    gather="awk -F: '$3>n&&$3<60000n=$3 ENDprint NR,n' /etc/passwd"

                    for server in $servers[@]; do
                    read count highest < <(ssh "$server" "$gather")
                    printf "$fmt" "$server" "$count" "$highest"
                    done


                    (Identical results.)






                    share|improve this answer













                    If you've got a key on each machine and can ssh remotehost from your monitoring host, you've got all that's required to collect the information you've asked for.



                    #!/bin/bash

                    servers=(wopr gerty mother)

                    fmt="%st%st%sn"
                    printf "$fmt" "Host" "UIDs" "Highest"
                    printf "$fmt" "----" "----" "-------"

                    count='awk "END print NR" /etc/passwd' # avoids whitespace problems from `wc`
                    highest="awk -F: '$3>n&&$3<60000n=$3 ENDprint n' /etc/passwd"

                    for server in $servers[@]; do
                    printf "$fmt" "$server" "$(ssh "$server" "$count")" "$(ssh "$server" "$highest")"
                    done


                    Results for me:



                    $ ./doit.sh
                    Host UIDs Highest
                    ---- ---- -------
                    wopr 40 2020
                    gerty 37 9001
                    mother 32 534


                    Note that this makes TWO ssh connections to each server to collect each datum. If you'd like to do this a little more efficiently, you can bundle the information into a single, slightly more complex collection script:



                    #!/usr/local/bin/bash

                    servers=(wopr gerty mother)

                    fmt="%st%st%sn"
                    printf "$fmt" "Host" "UIDs" "Highest"
                    printf "$fmt" "----" "----" "-------"

                    gather="awk -F: '$3>n&&$3<60000n=$3 ENDprint NR,n' /etc/passwd"

                    for server in $servers[@]; do
                    read count highest < <(ssh "$server" "$gather")
                    printf "$fmt" "$server" "$count" "$highest"
                    done


                    (Identical results.)







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Oct 17 '12 at 15:37









                    ghotighoti

                    36.7k7 gold badges46 silver badges89 bronze badges




                    36.7k7 gold badges46 silver badges89 bronze badges
























                        2














                        ssh remoteserver.example /bin/bash < localscript.bash






                        share|improve this answer





























                          2














                          ssh remoteserver.example /bin/bash < localscript.bash






                          share|improve this answer



























                            2












                            2








                            2







                            ssh remoteserver.example /bin/bash < localscript.bash






                            share|improve this answer













                            ssh remoteserver.example /bin/bash < localscript.bash







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 15 '13 at 17:44









                            c4f4t0rc4f4t0r

                            1,03311 silver badges22 bronze badges




                            1,03311 silver badges22 bronze badges
























                                0














                                (Note: the "proper" way to authenticate without manually entering in password is to use SSH keys. Storing password in plaintext even in your local scripts is a potential security vulnerability)



                                You can run expect as part of your bash script. Here's a quick example that you can hack into your existing script:



                                login=user
                                IP=127.0.0.1
                                password='your_password'

                                expect_sh=$(expect -c "
                                spawn ssh $login@$IP
                                expect "password:"
                                send "$passwordr"
                                expect "#"
                                send "./$remote_side_scriptr"
                                expect "#"
                                send "cd /libr"
                                expect "#"
                                send "cat file_namer"
                                expect "#"
                                send "exitr"
                                ")

                                echo "$expect_sh"


                                You can also use pscp to copy files back and forth as part of a script so you don't need to manually supply the password as part of the interaction:



                                Install putty-tools:



                                $ sudo apt-get install putty-tools


                                Using pscp in your script:



                                pscp -scp -pw $password file_to_copy $login@$IP:$dest_dir





                                share|improve this answer

























                                • unfortunately i have RedHat system (yum). Can't compile or install third-party tools :(

                                  – Satish
                                  Oct 17 '12 at 15:36















                                0














                                (Note: the "proper" way to authenticate without manually entering in password is to use SSH keys. Storing password in plaintext even in your local scripts is a potential security vulnerability)



                                You can run expect as part of your bash script. Here's a quick example that you can hack into your existing script:



                                login=user
                                IP=127.0.0.1
                                password='your_password'

                                expect_sh=$(expect -c "
                                spawn ssh $login@$IP
                                expect "password:"
                                send "$passwordr"
                                expect "#"
                                send "./$remote_side_scriptr"
                                expect "#"
                                send "cd /libr"
                                expect "#"
                                send "cat file_namer"
                                expect "#"
                                send "exitr"
                                ")

                                echo "$expect_sh"


                                You can also use pscp to copy files back and forth as part of a script so you don't need to manually supply the password as part of the interaction:



                                Install putty-tools:



                                $ sudo apt-get install putty-tools


                                Using pscp in your script:



                                pscp -scp -pw $password file_to_copy $login@$IP:$dest_dir





                                share|improve this answer

























                                • unfortunately i have RedHat system (yum). Can't compile or install third-party tools :(

                                  – Satish
                                  Oct 17 '12 at 15:36













                                0












                                0








                                0







                                (Note: the "proper" way to authenticate without manually entering in password is to use SSH keys. Storing password in plaintext even in your local scripts is a potential security vulnerability)



                                You can run expect as part of your bash script. Here's a quick example that you can hack into your existing script:



                                login=user
                                IP=127.0.0.1
                                password='your_password'

                                expect_sh=$(expect -c "
                                spawn ssh $login@$IP
                                expect "password:"
                                send "$passwordr"
                                expect "#"
                                send "./$remote_side_scriptr"
                                expect "#"
                                send "cd /libr"
                                expect "#"
                                send "cat file_namer"
                                expect "#"
                                send "exitr"
                                ")

                                echo "$expect_sh"


                                You can also use pscp to copy files back and forth as part of a script so you don't need to manually supply the password as part of the interaction:



                                Install putty-tools:



                                $ sudo apt-get install putty-tools


                                Using pscp in your script:



                                pscp -scp -pw $password file_to_copy $login@$IP:$dest_dir





                                share|improve this answer













                                (Note: the "proper" way to authenticate without manually entering in password is to use SSH keys. Storing password in plaintext even in your local scripts is a potential security vulnerability)



                                You can run expect as part of your bash script. Here's a quick example that you can hack into your existing script:



                                login=user
                                IP=127.0.0.1
                                password='your_password'

                                expect_sh=$(expect -c "
                                spawn ssh $login@$IP
                                expect "password:"
                                send "$passwordr"
                                expect "#"
                                send "./$remote_side_scriptr"
                                expect "#"
                                send "cd /libr"
                                expect "#"
                                send "cat file_namer"
                                expect "#"
                                send "exitr"
                                ")

                                echo "$expect_sh"


                                You can also use pscp to copy files back and forth as part of a script so you don't need to manually supply the password as part of the interaction:



                                Install putty-tools:



                                $ sudo apt-get install putty-tools


                                Using pscp in your script:



                                pscp -scp -pw $password file_to_copy $login@$IP:$dest_dir






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Oct 17 '12 at 15:30









                                sampson-chensampson-chen

                                34.2k10 gold badges67 silver badges69 bronze badges




                                34.2k10 gold badges67 silver badges69 bronze badges















                                • unfortunately i have RedHat system (yum). Can't compile or install third-party tools :(

                                  – Satish
                                  Oct 17 '12 at 15:36

















                                • unfortunately i have RedHat system (yum). Can't compile or install third-party tools :(

                                  – Satish
                                  Oct 17 '12 at 15:36
















                                unfortunately i have RedHat system (yum). Can't compile or install third-party tools :(

                                – Satish
                                Oct 17 '12 at 15:36





                                unfortunately i have RedHat system (yum). Can't compile or install third-party tools :(

                                – Satish
                                Oct 17 '12 at 15:36











                                0














                                maybe you'd like to try the expect command as following



                                #!/usr/bin/expect
                                set timeout 30
                                spawn ssh -p ssh_port -l ssh_username ssh_server_host
                                expect "password:"
                                send "your_passwdr"
                                interact


                                the expect command will catch the "password:" and then auto fill the passwd your send by above.



                                Remember that replace the ssh_port, ssh_username, ssh_server_host and your_passwd with your own configure






                                share|improve this answer





























                                  0














                                  maybe you'd like to try the expect command as following



                                  #!/usr/bin/expect
                                  set timeout 30
                                  spawn ssh -p ssh_port -l ssh_username ssh_server_host
                                  expect "password:"
                                  send "your_passwdr"
                                  interact


                                  the expect command will catch the "password:" and then auto fill the passwd your send by above.



                                  Remember that replace the ssh_port, ssh_username, ssh_server_host and your_passwd with your own configure






                                  share|improve this answer



























                                    0












                                    0








                                    0







                                    maybe you'd like to try the expect command as following



                                    #!/usr/bin/expect
                                    set timeout 30
                                    spawn ssh -p ssh_port -l ssh_username ssh_server_host
                                    expect "password:"
                                    send "your_passwdr"
                                    interact


                                    the expect command will catch the "password:" and then auto fill the passwd your send by above.



                                    Remember that replace the ssh_port, ssh_username, ssh_server_host and your_passwd with your own configure






                                    share|improve this answer













                                    maybe you'd like to try the expect command as following



                                    #!/usr/bin/expect
                                    set timeout 30
                                    spawn ssh -p ssh_port -l ssh_username ssh_server_host
                                    expect "password:"
                                    send "your_passwdr"
                                    interact


                                    the expect command will catch the "password:" and then auto fill the passwd your send by above.



                                    Remember that replace the ssh_port, ssh_username, ssh_server_host and your_passwd with your own configure







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 15 '13 at 14:54









                                    fayhotfayhot

                                    595 bronze badges




                                    595 bronze badges






























                                        draft saved

                                        draft discarded
















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • 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%2fstackoverflow.com%2fquestions%2f12937634%2frun-scripts-remotely-via-ssh%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

                                        Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                        Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                        Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript