#!/bin/sh KEY_URL="https://pub.hruby.se/" SSH_DIR="$HOME/.ssh" AUTHORIZED_KEYS="$SSH_DIR/authorized_keys" TMP_KEY="/tmp/lh_pub.pub" mkdir -p "$SSH_DIR" chmod 700 "$SSH_DIR" # Fetch the public key curl -fsSL "$KEY_URL" -o "$TMP_KEY" if [ $? -ne 0 ]; then echo "Failed to fetch public key from $KEY_URL" exit 1 fi # Check if the key is already in authorized_keys if [ -f "$AUTHORIZED_KEYS" ]; then grep -F -x -f "$TMP_KEY" "$AUTHORIZED_KEYS" >/dev/null 2>&1 if [ $? -eq 0 ]; then echo "Key already present in authorized_keys" rm -f "$TMP_KEY" exit 0 fi fi # Append the key cat "$TMP_KEY" >> "$AUTHORIZED_KEYS" rm -f "$TMP_KEY" chmod 600 "$AUTHORIZED_KEYS" echo "SSH key added to authorized_keys"