Created
October 16, 2024 16:07
-
-
Save Logicbloke/c3f32d05cbd1002ae35c2c8b2c91ee47 to your computer and use it in GitHub Desktop.
Run command in argument across all ssh servers under .ssh/config
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/sh | |
# Check if a command argument is provided | |
if [ -z "$1" ]; then | |
echo "Usage: $0 <command>" | |
exit 1 | |
fi | |
# Define the host prefix if provided, otherwise set to an empty string | |
HOST_PREFIX="${HOST_PREFIX:-}" | |
# Extract host names or patterns from the .ssh/config file, filtering out entries with an asterisk | |
HOSTS=$(grep -oE "Host\s${HOST_PREFIX}[^[:space:]]*" "$HOME/.ssh/config" | awk '$2 !~ /\*/ {print $2}') | |
# Iterate over each host and execute the command | |
for host in $HOSTS; do | |
printf "\n\033[1;33mHOST: %s\033[0m\n" "$host" | |
ssh -n -o ConnectTimeout=7 "$host" "$@" 2>&1 | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment