Created
January 26, 2026 14:21
-
-
Save vkuprin/653994e70fc382a9899c539063968716 to your computer and use it in GitHub Desktop.
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 | |
| # SSH Profile Manager - Easy switching between SSH configurations | |
| SSH_PROFILES_DIR="$HOME/.ssh-profiles" | |
| CURRENT_SSH="$HOME/.ssh" | |
| CURRENT_PROFILE_MARKER="$SSH_PROFILES_DIR/.current" | |
| GREEN='\033[0;32m' | |
| BLUE='\033[0;34m' | |
| YELLOW='\033[1;33m' | |
| RED='\033[0;31m' | |
| NC='\033[0m' # No Color | |
| init_profiles() { | |
| if [ ! -d "$SSH_PROFILES_DIR" ]; then | |
| mkdir -p "$SSH_PROFILES_DIR" | |
| echo -e "${GREEN}Created SSH profiles directory: $SSH_PROFILES_DIR${NC}" | |
| fi | |
| } | |
| list_profiles() { | |
| init_profiles | |
| echo -e "${BLUE}Available SSH profiles:${NC}" | |
| current_profile=$(cat "$CURRENT_PROFILE_MARKER" 2>/dev/null || echo "unknown") | |
| if [ -d "$CURRENT_SSH" ]; then | |
| echo -e " ${GREEN}* active${NC} (currently in use)" | |
| fi | |
| for profile in "$SSH_PROFILES_DIR"/*; do | |
| if [ -d "$profile" ]; then | |
| profile_name=$(basename "$profile") | |
| if [ "$profile_name" == "$current_profile" ]; then | |
| echo -e " ${GREEN}* $profile_name${NC} (current)" | |
| else | |
| echo " $profile_name" | |
| fi | |
| fi | |
| done | |
| } | |
| switch_profile() { | |
| local profile_name="$1" | |
| local profile_path="$SSH_PROFILES_DIR/$profile_name" | |
| if [ -z "$profile_name" ]; then | |
| echo -e "${RED}Error: Please specify a profile name${NC}" | |
| echo "Usage: ssh-profile switch <profile-name>" | |
| return 1 | |
| fi | |
| if [ ! -d "$profile_path" ]; then | |
| echo -e "${RED}Error: Profile '$profile_name' does not exist${NC}" | |
| echo "Available profiles:" | |
| list_profiles | |
| return 1 | |
| fi | |
| # Backup current .ssh if it exists and is not a symlink | |
| if [ -d "$CURRENT_SSH" ] && [ ! -L "$CURRENT_SSH" ]; then | |
| local timestamp=$(date +%Y%m%d_%H%M%S) | |
| local backup_name="backup_$timestamp" | |
| echo -e "${YELLOW}Backing up current .ssh to profile: $backup_name${NC}" | |
| mv "$CURRENT_SSH" "$SSH_PROFILES_DIR/$backup_name" | |
| elif [ -L "$CURRENT_SSH" ]; then | |
| rm "$CURRENT_SSH" | |
| fi | |
| echo -e "${GREEN}Switching to profile: $profile_name${NC}" | |
| cp -R "$profile_path" "$CURRENT_SSH" | |
| chmod 700 "$CURRENT_SSH" | |
| find "$CURRENT_SSH" -type f -name "id_*" -o -name "*_rsa" -o -name "*_ed25519" | xargs -I {} chmod 600 {} | |
| echo "$profile_name" > "$CURRENT_PROFILE_MARKER" | |
| echo -e "${GREEN}✓ Switched to profile: $profile_name${NC}" | |
| } | |
| save_profile() { | |
| local profile_name="$1" | |
| if [ -z "$profile_name" ]; then | |
| echo -e "${RED}Error: Please specify a profile name${NC}" | |
| echo "Usage: ssh-profile save <profile-name>" | |
| return 1 | |
| fi | |
| init_profiles | |
| local profile_path="$SSH_PROFILES_DIR/$profile_name" | |
| if [ -d "$profile_path" ]; then | |
| read -p "Profile '$profile_name' already exists. Overwrite? (y/N) " -n 1 -r | |
| echo | |
| if [[ ! $REPLY =~ ^[Yy]$ ]]; then | |
| echo "Cancelled." | |
| return 1 | |
| fi | |
| rm -rf "$profile_path" | |
| fi | |
| if [ ! -d "$CURRENT_SSH" ]; then | |
| echo -e "${RED}Error: No .ssh directory found${NC}" | |
| return 1 | |
| fi | |
| cp -R "$CURRENT_SSH" "$profile_path" | |
| echo "$profile_name" > "$CURRENT_PROFILE_MARKER" | |
| echo -e "${GREEN}✓ Saved current .ssh as profile: $profile_name${NC}" | |
| } | |
| current_profile() { | |
| if [ -f "$CURRENT_PROFILE_MARKER" ]; then | |
| local profile=$(cat "$CURRENT_PROFILE_MARKER") | |
| echo -e "${GREEN}Current profile: $profile${NC}" | |
| else | |
| echo -e "${YELLOW}No profile is currently active${NC}" | |
| fi | |
| } | |
| delete_profile() { | |
| local profile_name="$1" | |
| local profile_path="$SSH_PROFILES_DIR/$profile_name" | |
| if [ -z "$profile_name" ]; then | |
| echo -e "${RED}Error: Please specify a profile name${NC}" | |
| echo "Usage: ssh-profile delete <profile-name>" | |
| return 1 | |
| fi | |
| if [ ! -d "$profile_path" ]; then | |
| echo -e "${RED}Error: Profile '$profile_name' does not exist${NC}" | |
| return 1 | |
| fi | |
| read -p "Delete profile '$profile_name'? (y/N) " -n 1 -r | |
| echo | |
| if [[ $REPLY =~ ^[Yy]$ ]]; then | |
| rm -rf "$profile_path" | |
| echo -e "${GREEN}✓ Deleted profile: $profile_name${NC}" | |
| fi | |
| } | |
| show_help() { | |
| echo "SSH Profile Manager - Easily manage multiple SSH configurations" | |
| echo "" | |
| echo "Usage: ssh-profile <command> [arguments]" | |
| echo "" | |
| echo "Commands:" | |
| echo " list List all saved profiles" | |
| echo " switch <name> Switch to a different profile" | |
| echo " save <name> Save current .ssh as a profile" | |
| echo " current Show current active profile" | |
| echo " delete <name> Delete a saved profile" | |
| echo " help Show this help message" | |
| echo "" | |
| echo "Examples:" | |
| echo " ssh-profile save amazon # Save current .ssh as 'amazon'" | |
| echo " ssh-profile save personal # Save current .ssh as 'personal'" | |
| echo " ssh-profile list # List all profiles" | |
| echo " ssh-profile switch amazon # Switch to 'amazon' profile" | |
| } | |
| case "$1" in | |
| list|ls) | |
| list_profiles | |
| ;; | |
| switch|use) | |
| switch_profile "$2" | |
| ;; | |
| save|add) | |
| save_profile "$2" | |
| ;; | |
| current) | |
| current_profile | |
| ;; | |
| delete|rm) | |
| delete_profile "$2" | |
| ;; | |
| help|--help|-h|"") | |
| show_help | |
| ;; | |
| *) | |
| echo -e "${RED}Unknown command: $1${NC}" | |
| show_help | |
| exit 1 | |
| ;; | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment