Skip to content

Instantly share code, notes, and snippets.

@RMcGhee
Created April 25, 2024 19:06
Show Gist options
  • Select an option

  • Save RMcGhee/7a9ab9223a736005582cc1a768a20ea9 to your computer and use it in GitHub Desktop.

Select an option

Save RMcGhee/7a9ab9223a736005582cc1a768a20ea9 to your computer and use it in GitHub Desktop.
Show your ssh configs in a table format
#!/bin/bash
# Path to the SSH config file
config_file="$HOME/.ssh/config"
# Check if the SSH config file exists
if [[ ! -f "$config_file" ]]; then
echo "SSH config file not found at $config_file"
exit 1
fi
# Function to print an entry with padded fields
print_entry() {
printf "%-17s | %-17s | %-17s | %-17s\n" "$1" "$2" "$3" "$4"
}
# Initialize variables for the first Host entry
host=""
hostname=""
user=""
forward_agent=""
# Read the file line by line
printf "\n%-17s | %-17s | %-17s | %-17s \n" "Host" "HostName" "User" "ForwardAgent"
echo "-----------------------------------------------------------------------------"
while IFS= read -r line; do
# Trim leading whitespace
trimmed_line="$(echo "$line" | sed -e 's/^[ \t]*//')"
# Check for a Host entry
if [[ "$trimmed_line" =~ ^Host\ +(.+) ]]; then
# If it's not the first entry, print the details of the previous entry
if [[ -n "$host" ]]; then
print_entry "$host" "${hostname:-___}" "${user:-___}" "${forward_agent:-___}"
fi
# Reset variables for the new Host entry
host="${BASH_REMATCH[1]}"
hostname="___"
user="___"
forward_agent="___"
# Check for Hostname, User, and ForwardAgent entries
elif [[ "$trimmed_line" =~ ^HostName\ +(.+) ]]; then
hostname="${BASH_REMATCH[1]}"
elif [[ "$trimmed_line" =~ ^User\ +(.+) ]]; then
user="${BASH_REMATCH[1]}"
elif [[ "$trimmed_line" =~ ^ForwardAgent\ +(.+) ]]; then
forward_agent="${BASH_REMATCH[1]}"
fi
done < "$config_file"
# Print the details of the last entry
if [[ -n "$host" ]]; then
print_entry "$host" "${hostname:-___}" "${user:-___}" "${forward_agent:-___}"
fi
@ericpoe
Copy link
Copy Markdown

ericpoe commented Apr 25, 2024

This is really handy. Thanks!

@sloan58
Copy link
Copy Markdown

sloan58 commented Apr 30, 2024

Fantastic! Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment