How to execute linux commands in a remote machine with shell script

Keerthisairam

I have a test server, from which i need to login to two different prod server and execute top, free -h, ps -ef commands could some one help me with a shell script

Weike

Only ssh command can do this:

$ for ip in 192.168.138.22{1,2,3}; do ssh ${ip} -o StrictHostKeyChecking=no "free -h"; done
              total        used        free      shared  buff/cache   available
Mem:           7.8G        782M        5.2G        152M        1.8G        6.4G
Swap:          7.9G          0B        7.9G
              total        used        free      shared  buff/cache   available
Mem:           7.8G        1.3G        4.1G        169M        2.5G        5.8G
Swap:          7.9G          0B        7.9G
              total        used        free      shared  buff/cache   available
Mem:           7.8G        563M        5.9G        118M        1.4G        6.6G
Swap:          7.9G          0B        7.9G

However, this need to authenticate ssh-key with each other. So, we can use sshpass to pass the passqord:

$ for ip in 192.168.138.22{1,2,3}; do sshpass -p PASSWORD ssh ${ip} -o StrictHostKeyChecking=no "free -h"; done
              total        used        free      shared  buff/cache   available
Mem:           7.8G        782M        5.2G        152M        1.8G        6.4G
Swap:          7.9G          0B        7.9G
              total        used        free      shared  buff/cache   available
Mem:           7.8G        1.3G        4.1G        169M        2.5G        5.8G
Swap:          7.9G          0B        7.9G
              total        used        free      shared  buff/cache   available
Mem:           7.8G        563M        5.9G        118M        1.4G        6.6G
Swap:          7.9G          0B        7.9G

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Triggering a shell script on remote machine

From Dev

How to execute remote pysftp commands under a specific shell

From Dev

How to execute the vim commands through shell script

From Dev

How to execute a shell script on a remote server using Ansible?

From Dev

golang: Execute shell commands on remote server

From Dev

Cannot execute shell commands in bash script

From Dev

How to execute a shell script stored on remote machine from a local machine?

From Dev

Execute Shell script from workspace on remote machine after build successful (Jenkins)

From Dev

Execute shell script in remote machine using ssh command with config file

From Dev

How to execute commands in docker container as part of bash shell script

From Dev

How does Linux know how to execute shell vs machine code?

From Dev

Executing commands with ssh and shell script using variables on a remote machine

From Dev

How does Linux know how to execute shell vs machine code?

From Dev

How can I execute local script on remote machine and include arguments?

From Dev

How do I execute a remote shell script over SSH and be prompted for passwords by commands that require it in that script?

From Dev

Executing commands with ssh and shell script using variables on a remote machine

From Dev

How to execute remote pysftp commands under a specific shell

From Dev

How execute an AutoIt script on remote machine

From Dev

How to execute shell commands in a loop from within a bash script?

From Dev

How to delete a file on remote machine via SSH by using a Shell Script?

From Dev

Make a shell script execute commands in telnet or programs

From Dev

How to execute commands in gnuplot using shell script?

From Dev

linux + run script on remote machine

From Dev

How to execute bash script on a remote machine asynchronously

From Dev

Execute command inside a shell script on a remote machine and get output on local machine

From Dev

how to execute compound commands in shell script?

From Dev

Login to remote Linux box and execute commands

From Dev

Execute Script available on remote machine

From Dev

How to execute shell script?

Related Related

  1. 1

    Triggering a shell script on remote machine

  2. 2

    How to execute remote pysftp commands under a specific shell

  3. 3

    How to execute the vim commands through shell script

  4. 4

    How to execute a shell script on a remote server using Ansible?

  5. 5

    golang: Execute shell commands on remote server

  6. 6

    Cannot execute shell commands in bash script

  7. 7

    How to execute a shell script stored on remote machine from a local machine?

  8. 8

    Execute Shell script from workspace on remote machine after build successful (Jenkins)

  9. 9

    Execute shell script in remote machine using ssh command with config file

  10. 10

    How to execute commands in docker container as part of bash shell script

  11. 11

    How does Linux know how to execute shell vs machine code?

  12. 12

    Executing commands with ssh and shell script using variables on a remote machine

  13. 13

    How does Linux know how to execute shell vs machine code?

  14. 14

    How can I execute local script on remote machine and include arguments?

  15. 15

    How do I execute a remote shell script over SSH and be prompted for passwords by commands that require it in that script?

  16. 16

    Executing commands with ssh and shell script using variables on a remote machine

  17. 17

    How to execute remote pysftp commands under a specific shell

  18. 18

    How execute an AutoIt script on remote machine

  19. 19

    How to execute shell commands in a loop from within a bash script?

  20. 20

    How to delete a file on remote machine via SSH by using a Shell Script?

  21. 21

    Make a shell script execute commands in telnet or programs

  22. 22

    How to execute commands in gnuplot using shell script?

  23. 23

    linux + run script on remote machine

  24. 24

    How to execute bash script on a remote machine asynchronously

  25. 25

    Execute command inside a shell script on a remote machine and get output on local machine

  26. 26

    how to execute compound commands in shell script?

  27. 27

    Login to remote Linux box and execute commands

  28. 28

    Execute Script available on remote machine

  29. 29

    How to execute shell script?

HotTag

Archive