Skip to content

Instantly share code, notes, and snippets.

@mjcc30
Last active September 28, 2023 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjcc30/f04196a63f3458315a8498e92d73137a to your computer and use it in GitHub Desktop.
Save mjcc30/f04196a63f3458315a8498e92d73137a to your computer and use it in GitHub Desktop.
script to easily connect to kodekloud servers
#!/bin/bash
declare -A servers=(
["stapp01"]="tony:172.16.238.10:Ir0nM@n:App server 1"
["stapp02"]="steve:172.16.238.11:Am3ric@:App server 2"
["stapp03"]="banner:172.16.238.12:BigGr33n:App server 3"
["stlb01"]="loki:172.16.238.14:Mischi3f:HTTP LBR"
["stdb01"]="peter:172.16.239.10:Sp!dy:DB Server"
["ststor01"]="natasha:172.16.238.15:Bl@kW:Storage Server"
["stbkp01"]="clint:172.16.238.16:H@wk3y3:Backup Server"
["stmail01"]="groot:172.16.238.17:Gr00T123:Mail Server"
["jump_host"]="thor:jump_host.stratos.xfusioncorp.com:mjolnir123:Jump Server"
["jenkins"]="jenkins:172.16.238.19:j@rv!s:Jenkins Server "
)
# Function to print available Servers
print_available_servers() {
server_list=()
echo
echo "Available Servers in Stratos Datacenter:"
for server_name in "${!servers[@]}"; do
server_info="${servers[$server_name]}"
IFS=':' read -r -a server_info_arr <<< "$server_info"
purpose="${server_info_arr[3]}"
server_list+=("$purpose: $server_name")
done
echo
echo "$(printf '%s\n' "${server_list[@]}" | sort)"
exit 1
}
# Check if the server name is provided as an argument
if [ $# -eq 0 ]; then
echo "Usage: $0 <Server Name>"
print_available_servers
fi
# Check if the provided server name is valid
server_name="$1"
if [ -z "${servers[$server_name]}" ]; then
echo "Invalid server name: $server_name"
available_servers
fi
# Function to check if sshpass is installed
check_sshpass() {
if ! command -v sshpass &> /dev/null; then
echo "sshpass is not installed."
# Detect the OS and use the appropriate package manager to install sshpass
if [[ "$OSTYPE" == "linux-gnu" ]]; then
# Linux-based system
if command -v apt-get &> /dev/null; then
echo "sudo apt-get install -y sshpass"
exit 1
elif command -v yum &> /dev/null; then
echo "sudo yum install -y sshpass"
exit 1
elif command -v pacman &> /dev/null; then
echo "sudo pacman -S --noconfirm sshpass"
exit 1
else
echo "Unsupported Linux distribution."
exit 1
fi
else
echo "Unsupported operating system."
exit 1
fi
else
echo "sshpass is already installed."
fi
}
# Check if sshpass is installed
check_sshpass
# Extract username, IP, and password from the server details
server_info="${servers[$server_name]}"
IFS=':' read -r -a server_info_arr <<< "$server_info"
username_ip="${server_info_arr[0]}"
IFS='@' read -r -a username_ip_arr <<< "$username_ip"
username="${username_ip_arr[0]}"
ip="${server_info_arr[1]}"
password="${server_info_arr[2]}"
purpose="${server_info_arr[3]}"
# Echo extracted values
echo "Purpose: $purpose"
echo "Server Name: $server_name"
echo "Username: $username"
echo "IP Address: $ip"
echo "Password: $password"
# SSH into the selected server using sshpass
sshpass -p "$password" ssh -o StrictHostKeyChecking=no "$username@$ip"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment