-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_ssh_setup.sh
More file actions
179 lines (151 loc) · 4.19 KB
/
Copy pathauto_ssh_setup.sh
File metadata and controls
179 lines (151 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/bash
# GitHub Repository: https://github.com/Newton-Nganga
# Function to log messages
log() {
echo -e "\n[LOG] $1"
}
# Function to log errors
error() {
echo -e "\n[ERROR] $1"
}
# Function to log success messages
success() {
echo -e "\n[SUCCESS] $1"
}
# Function to display a header
display_header() {
clear
echo "=============================================="
echo " SSH Setup Script for GitHub/Bitbucket"
echo " Created by: Newton-Nganga"
echo " GitHub: https://github.com/Newton-Nganga"
echo "=============================================="
echo ""
}
# Function to check and set Git global username and email
setup_git_config() {
local git_user=$(git config --global user.name)
local git_email=$(git config --global user.email)
if [ -z "$git_user" ]; then
log "Git global username is not set."
read -p "Enter your Git username: " git_user
git config --global user.name "$git_user"
fi
if [ -z "$git_email" ]; then
log "Git global email is not set."
read -p "Enter your Git email: " git_email
git config --global user.email "$git_email"
fi
success "Git global username and email are set up!"
}
# Function to create or clear the ~/.ssh directory
setup_ssh_directory() {
if [ -d ~/.ssh ]; then
log "~/.ssh directory already exists. Clearing contents..."
rm -rf ~/.ssh/*
else
log "Creating ~/.ssh directory..."
mkdir -p ~/.ssh
fi
chmod 700 ~/.ssh
}
# Function to generate SSH keys
generate_ssh_key() {
local key_name=$1
local email=$2
log "Generating SSH key for $email..."
ssh-keygen -t ed25519 -C "$email" -f ~/.ssh/"$key_name"
chmod 600 ~/.ssh/"$key_name"
chmod 644 ~/.ssh/"$key_name.pub"
success "SSH key generated successfully!"
}
# Function to add SSH key to the config file
add_to_config() {
local key_name=$1
local host=$2
log "Adding $key_name to SSH config for $host..."
cat <<EOF >> ~/.ssh/config
Host $host
HostName $host
IdentityFile ~/.ssh/$key_name
User git
EOF
success "SSH config updated for $host!"
}
# Function to display the public key
display_public_key() {
local key_name=$1
log "Here is your public key. Copy and add it to your account:"
echo -e "\n$(cat ~/.ssh/"$key_name.pub")\n"
}
# Function to test the SSH connection
test_ssh_connection() {
local host=$1
log "Testing SSH connection to $host..."
ssh -T git@$host
if [ $? -eq 0 ]; then
success "SSH connection to $host is working!"
else
error "SSH connection to $host failed. Please check your setup."
fi
}
# Main script logic
display_header
# Check and set Git global username and email
setup_git_config
# Ask the user to choose a platform
PS3="Select the platform you want to set up SSH for: "
options=("GitHub" "Bitbucket" "Quit")
select opt in "${options[@]}"; do
case $opt in
"GitHub")
platform="github.com"
break
;;
"Bitbucket")
platform="bitbucket.org"
break
;;
"Quit")
log "Exiting the script. Goodbye!"
exit 0
;;
*)
error "Invalid option. Please try again."
;;
esac
done
# Set up the SSH directory
setup_ssh_directory
# Ask for the key name
log "Please provide a name for your SSH key (e.g., id_github):"
read -r key_name
# Ask for the email associated with the account
log "Please enter the email associated with your $platform account:"
read -r email
# Generate the SSH key
generate_ssh_key "$key_name" "$email"
# Add the key to the SSH config
add_to_config "$key_name" "$platform"
# Display the public key
display_public_key "$key_name"
# Wait for the user to add the key to their account
log "Please add the public key to your $platform account. Once done, type 'yes' to continue."
while true; do
read -r response
if [ "$response" = "yes" ]; then
break
else
error "Invalid input. Please type 'yes' to continue."
fi
done
# Test the SSH connection
test_ssh_connection "$platform"
# Guide the user on cloning a repository
log "To clone a repository, use the following command:"
echo -e "\ngit clone git@$platform:username/repository.git\n"
# Change directory to ~
cd ~ || exit
# End of script
success "SSH setup completed successfully! You are now in the ~ directory."
log "Script terminated. Goodbye!"