From 3541229206312948632f6c8d6d0fe9c7787fa5af Mon Sep 17 00:00:00 2001 From: siujamo Date: Thu, 4 Jun 2026 14:27:15 +0800 Subject: [PATCH] feat: a script add public keys from github --- Debian/import-pubkeys-from-github.sh | 62 ++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Debian/import-pubkeys-from-github.sh diff --git a/Debian/import-pubkeys-from-github.sh b/Debian/import-pubkeys-from-github.sh new file mode 100644 index 0000000..6de397c --- /dev/null +++ b/Debian/import-pubkeys-from-github.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +# Ensure a list of usernames is provided +if [ -z "$1" ]; then + echo "Error: No usernames provided." + echo "Usage: bash $0 username1,username2" + exit 1 +fi + +TARGET_FILE="$HOME/.ssh/authorized_keys" + +# Initialising: Ensure .ssh directory exists with correct secure permissions +mkdir -p "$HOME/.ssh" +chmod 700 "$HOME/.ssh" + +# Ensure the authorised keys file exists so we can safely check its content later +touch "$TARGET_FILE" +chmod 600 "$TARGET_FILE" + +# Split the first argument by commas into an array +IFS=',' read -r -a USERNAMES <<< "$1" + +for USERNAME in "${USERNAMES[@]}"; do + # Trim potential leading/trailing whitespaces from username + USERNAME=$(echo "$USERNAME" | xargs) + + if [ -z "$USERNAME" ]; then + continue + fi + + echo "Fetching SSH public keys for GitHub user: ${USERNAME}..." + + # Fetch keys from GitHub API (following redirects) + KEYS=$(curl -sSL "https://github.com/${USERNAME}.keys") + + # Check if the user exists or has any public keys + if [ -z "$KEYS" ] || [[ "$KEYS" == *"Not Found"* ]]; then + echo "Warning: No public keys found for user '${USERNAME}' or user does not exist. Skipping." + continue + fi + + # Format the header exactly as required + HEADER="# === SSH PUB KEY FOR ${USERNAME} ===" + + # Check if this specific header already exists to avoid duplicate entries + if grep -qF "$HEADER" "$TARGET_FILE"; then + echo "Notice: Keys for '${USERNAME}' have already been imported previously. Skipping to prevent duplicates." + continue + fi + + # Append the formatted block to the authorised keys file safely + # The awk block ensures that the keys always end with a proper newline character + { + echo "" + echo "$HEADER" + echo "$KEYS" | awk '1; END {if (NR && !/\n$/) print ""}' + } >> "$TARGET_FILE" + + echo "Successfully appended keys for ${USERNAME}." +done + +echo "SSH key import process completed." \ No newline at end of file