Created
December 4, 2024 22:52
-
-
Save shahpnmlab/eb7c491e244951aafaaf76e324bf7397 to your computer and use it in GitHub Desktop.
script to manage different ssh accounts
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 | |
function ssh_manager { | |
local HOSTS=("scan" "rc1" "rc2" "rc3" "rc4" "pinot") | |
local HOST="" | |
local CONTROL_PATH="$HOME/.ssh/control:%h:%p:%r" | |
# Parse command line arguments | |
if [[ $1 == "-h" || $1 == "--help" ]]; then | |
echo "Usage: sshmgr [host]" | |
echo " host: The host to connect to (scan, rc1, rc2, rc3, rc4 or pinot). If not provided, you'll be prompted." | |
exit 0 | |
elif [[ " ${HOSTS[@]} " =~ " $1 " ]]; then | |
HOST=$1 | |
fi | |
# If no host is specified, prompt the user | |
if [[ -z $HOST ]]; then | |
echo "Select a host:" | |
select HOST in "${HOSTS[@]}"; do | |
if [[ " ${HOSTS[@]} " =~ " ${HOST} " ]]; then | |
break | |
else | |
echo "Invalid selection. Please try again." | |
fi | |
done | |
fi | |
# Check if an existing control connection exists | |
if ssh -O check $HOST 2>/dev/null; then | |
echo "Existing SSH control connection found. Connecting..." | |
else | |
echo "No existing SSH control connection found. Establishing a new one..." | |
ssh -fN $HOST | |
if [ $? -ne 0 ]; then | |
echo "Error: Failed to establish control connection." | |
exit 1 | |
fi | |
echo "Control connection established successfully." | |
fi | |
# Start an interactive SSH session | |
ssh $HOST | |
} | |
# Call the main function when the script is executed | |
ssh_manager "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment