Files
doczyai-pipelines/on_demand_scripts/setup_ssh_auth.sh
T

94 lines
2.2 KiB
Bash
Raw Normal View History

#!/bin/bash
# Define variables
EMAIL="your_email@example.com" # Replace with your email
SSH_KEY_NAME="id_ed25519_bitbucket" # Replace with your desired key name
# Function to display messages
function info {
echo -e "\033[1;34m[INFO] $1\033[0m"
}
function success {
echo -e "\033[1;32m[SUCCESS] $1\033[0m"
}
function error {
echo -e "\033[1;31m[ERROR] $1\033[0m" >&2
}
# Update package list and install OpenSSH client
info "Updating package list and installing OpenSSH client..."
if sudo apt update && sudo apt install -y openssh-client; then
success "OpenSSH client installed successfully."
else
error "Failed to install OpenSSH client."
exit 1
fi
# Start the SSH agent
info "Starting the SSH agent..."
if eval "$(ssh-agent)"; then
success "SSH agent started."
else
error "Failed to start SSH agent."
exit 1
fi
# Navigate to the home directory
cd ~
# Generate the SSH key
info "Generating SSH key..."
if ssh-keygen -t ed25519 -b 4096 -C "$EMAIL" -f ~/.ssh/"$SSH_KEY_NAME"; then
success "SSH key generated at ~/.ssh/$SSH_KEY_NAME."
else
error "Failed to generate SSH key."
exit 1
fi
# Add the SSH key to the agent
info "Adding SSH key to the agent..."
if ssh-add ~/.ssh/"$SSH_KEY_NAME"; then
success "SSH key added to the agent."
else
error "Failed to add SSH key to the agent."
exit 1
fi
# Create .ssh directory if it doesn't exist
if [ ! -d ~/.ssh ]; then
mkdir -p ~/.ssh
chmod 700 ~/.ssh
fi
# Create or update the SSH config file
info "Configuring SSH for Bitbucket..."
SSH_CONFIG_PATH=~/.ssh/config
{
echo "Host bitbucket.org"
echo " AddKeysToAgent yes"
echo " IdentityFile ~/.ssh/$SSH_KEY_NAME"
} | sudo tee -a $SSH_CONFIG_PATH > /dev/null
if [ $? -eq 0 ]; then
success "SSH configuration updated."
else
error "Failed to update SSH configuration."
exit 1
fi
# Set correct permissions for the config file
sudo chmod 600 $SSH_CONFIG_PATH
# Test the SSH connection to Bitbucket
info "Testing SSH connection to Bitbucket..."
if ssh -T git@bitbucket.org; then
success "SSH authentication with Bitbucket succeeded."
else
error "SSH authentication with Bitbucket failed."
exit 1
fi
success "SSH setup for Bitbucket completed successfully."