Skip to content

Instantly share code, notes, and snippets.

@rviki84
Created December 13, 2016 13:15
Show Gist options
  • Select an option

  • Save rviki84/20531a0b2d343b79ae8ec27e7bce33a1 to your computer and use it in GitHub Desktop.

Select an option

Save rviki84/20531a0b2d343b79ae8ec27e7bce33a1 to your computer and use it in GitHub Desktop.
SSH Multi-Host Execution
198.209.202.11
198.31.175.22
198.209.133.33
#!/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