-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.sh
executable file
·48 lines (40 loc) · 1.12 KB
/
keys.sh
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
#!/bin/bash
# Function to display usage information
usage() {
echo "Usage: $0 [-u <username>]"
exit 1
}
# Parse command-line options
while getopts "u:" opt; do
case $opt in
u)
USERNAME="$OPTARG"
;;
\?)
usage
;;
esac
done
# If the username is not provided as a flag, prompt the user
if [ -z "$USERNAME" ]; then
read -p "Enter your name: " USERNAME
# Validate that the username is not empty
if [ -z "$USERNAME" ]; then
echo "Error: Username cannot be empty."
exit 1
fi
fi
# Define the URL with the provided username
AUTHORIZED_KEYS_URL="https://github.com/$USERNAME.keys"
# Create the ~/.ssh/ directory if it doesn't exist
mkdir -p ~/.ssh/
# Use curl to download the file and append its contents to authorized_keys
curl -s "$AUTHORIZED_KEYS_URL" >> ~/.ssh/authorized_keys
# Check if curl was successful
if [ $? -eq 0 ]; then
echo "Authorized keys successfully downloaded and appended."
exit 0 # Exit with success code
else
echo "Error: Unable to download authorized keys."
exit 1 # Exit with failure code
fi