diff --git a/import_keys.sh b/import_keys.sh new file mode 100644 index 0000000..eae38be --- /dev/null +++ b/import_keys.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# Configuration +URL="https://git.1337733.xyz/martin/sshconfig/raw/branch/main/authorized_keys" +LOCAL_FILE="$HOME/.ssh/authorized_keys" + +# Ensure .ssh exists +mkdir -p ~/.ssh && chmod 700 ~/.ssh +touch "$LOCAL_FILE" + +echo "Checking for new keys..." + +# 1. Download keys to a temporary file +temp_keys=$(mktemp) +curl -s "$URL" > "$temp_keys" + +# 2. Add a newline to the local file to prevent "glued" keys +echo "" >> "$LOCAL_FILE" + +# 3. Combine files and remove duplicates while keeping order +# This 'awk' magic keeps the first occurrence of every line and ignores the rest. +# It preserves your comments and their relationship to the keys. +cp "$LOCAL_FILE" "${LOCAL_FILE}.bak" +cat "$LOCAL_FILE" "$temp_keys" | awk '!visited[$0]++' > "${LOCAL_FILE}.tmp" && mv "${LOCAL_FILE}.tmp" "$LOCAL_FILE" + +# 4. Final Cleanup +chmod 600 "$LOCAL_FILE" +rm "$temp_keys" + +echo "Done! New keys (if any) have been added. A backup was created at authorized_keys.bak." \ No newline at end of file