Created
January 10, 2025 05:45
-
-
Save dongfg/4dd9cd47cc1ae6c87bf984c36ac81e3b to your computer and use it in GitHub Desktop.
Install Authorized Keys From Github
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 | |
GITHUB_USERNAME="dongfg" | |
AUTHORIZED_KEYS_FILE="$HOME/.ssh/authorized_keys" | |
ALLOWED_KEY_TYPE="ssh-ed25519" | |
if [ ! -d "$HOME/.ssh" ]; then | |
mkdir -p "$HOME/.ssh" | |
chmod 700 "$HOME/.ssh" | |
fi | |
PUB_KEYS=$(curl -s "https://github.com/${GITHUB_USERNAME}.keys" | grep "^$ALLOWED_KEY_TYPE") | |
if [ -z "$PUB_KEYS" ]; then | |
echo "No valid '$ALLOWED_KEY_TYPE' public keys found for GitHub user: $GITHUB_USERNAME" | |
exit 1 | |
fi | |
echo "Processing public keys of type '$ALLOWED_KEY_TYPE' for user '$GITHUB_USERNAME'..." | |
ADDED_COUNT=0 | |
while read -r KEY; do | |
if grep -Fxq "$KEY" "$AUTHORIZED_KEYS_FILE"; then | |
echo "Key already exists: $KEY" | |
else | |
echo "$KEY" >> "$AUTHORIZED_KEYS_FILE" | |
echo "Key added: $KEY" | |
ADDED_COUNT=$((ADDED_COUNT + 1)) | |
fi | |
done <<< "$PUB_KEYS" | |
chmod 600 "$AUTHORIZED_KEYS_FILE" | |
if [ $ADDED_COUNT -eq 0 ]; then | |
echo "No new keys were added." | |
else | |
echo "$ADDED_COUNT key(s) of type '$ALLOWED_KEY_TYPE' added to $AUTHORIZED_KEYS_FILE." | |
fi | |
echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment