Created
December 13, 2016 13:15
-
-
Save rviki84/20531a0b2d343b79ae8ec27e7bce33a1 to your computer and use it in GitHub Desktop.
SSH Multi-Host Execution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 198.209.202.11 | |
| 198.31.175.22 | |
| 198.209.133.33 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # | |
| # Run a command on several remote hosts via SSH connection. <ps -fe | grep 'string'> is used as example in this demo script. | |
| # The list of hosts(IPs) is to be supplied as one of the arguments. Each IP in a single line followed by line feed (Enter key). | |
| # Uses the SSH Public-Key authentication method to login as particular USER. | |
| # | |
| SEARCH_TEXT=${1:-processName} | |
| USER=${2:-ubuntu} | |
| SSH_PRIVATE_KEY=${3:-"path/to/your/ssh/private/key"} | |
| HOSTS=${4:-"path/to/the/hostList.txt"} | |
| for host in $(cat $HOSTS); do | |
| #echo "Host IP:$host" | |
| ssh -i $SSH_PRIVATE_KEY -o StrictHostKeyChecking=no $USER@$host ps -fe | grep $SEARCH_TEXT > "output_${host}.txt" | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment