#!/bin/sh today=$(date '+%Y_%m_%d__%H_%M_%S') DIR="$HOME/.ssh" FILE="$DIR/authorized_keys" GITHUB_USER="sitemap.xml" echo "Fetching SSH keys for GitHub user $GITHUB_USER" # Fetch keys from GitHub API GITHUB_KEYS=$(curl -s "https://api.github.com/users/$GITHUB_USER/keys" | awk -F'"' '/"key":/ {print $4}') # Check if the curl command was successful if [ $? -ne 0 ]; then echo "Failed to fetch SSH keys from GitHub" exit 1 fi # Ensure directory exists if [ ! -d "$DIR" ]; then echo "Creating $DIR" mkdir -p "$DIR" || { echo "Failed to create directory $DIR"; exit 1; } fi # Ensure authorized_keys exists if [ -f "$FILE" ]; then BACKUP="$FILE.$today.backup" echo "Backing up existing $FILE to $BACKUP" cp "$FILE" "$BACKUP" || { echo "Failed to backup $FILE"; exit 1; } else echo "Creating $FILE" touch "$FILE" || { echo "Failed to create $FILE"; exit 1; } fi # Ensure appropriate permissions chmod 600 "$FILE" || { echo "Failed to set permissions on $FILE"; exit 1; } # Add GitHub keys to authorized_keys for key in $GITHUB_KEYS; do if grep -q "$key" "$FILE"; then echo "Key exists, skipping" else echo "Adding GitHub key" echo "$key" >> "$FILE" || { echo "Failed to add key to $FILE"; exit 1; } fi done echo "Done!"