#!/bin/bash # Script to add SSH public key to remote server using plink (PuTTY) # Usage: ./add-ssh-key.sh set -e REMOTE_HOST="155.133.129.88" REMOTE_USER="admin" REMOTE_PASSWORD="GHDKkpNUNuFePQb4KZ4%" SSH_KEY="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFURI5emj/BtYj6fgO6JnqH8csxJeSlyWkLs1DcPdoVp titouan.tabere@gmail.com" echo "Adding SSH key to ${REMOTE_USER}@${REMOTE_HOST} using plink..." # Remote script to execute REMOTE_SCRIPT="mkdir -p ~/.ssh && chmod 700 ~/.ssh && AUTH_KEYS_FILE=\$HOME/.ssh/authorized_keys && KEY_TO_ADD='${SSH_KEY}' && if [ -f \"\$AUTH_KEYS_FILE\" ] && grep -qF \"\$KEY_TO_ADD\" \"\$AUTH_KEYS_FILE\"; then echo 'SSH key already exists in authorized_keys'; else echo \"\$KEY_TO_ADD\" >> \"\$AUTH_KEYS_FILE\" && echo 'SSH key added successfully'; fi && chmod 600 \"\$AUTH_KEYS_FILE\" && echo 'SSH key setup completed'" # Check if plink is available if command -v plink &> /dev/null || [ -f "/c/Program Files/PuTTY/plink.exe" ]; then PLINK_CMD="plink" if [ -f "/c/Program Files/PuTTY/plink.exe" ]; then PLINK_CMD="/c/Program Files/PuTTY/plink.exe" fi echo "Using plink for authentication..." # Use plink with -pw flag to pass password # Accept host key using the fingerprint provided "$PLINK_CMD" -ssh -pw "${REMOTE_PASSWORD}" -batch -hostkey "SHA256:QtU+b4Fx3PSYDUwrgpXcZKZQCe9N8yZWnxY43Wh/bUA" \ "${REMOTE_USER}@${REMOTE_HOST}" "${REMOTE_SCRIPT}" else echo "Error: plink not found." echo "Please ensure PuTTY is installed or use the manual method below." echo "" echo "Manual method - run these commands:" echo " ssh ${REMOTE_USER}@${REMOTE_HOST}" echo "" echo "Then execute on the remote server:" echo " mkdir -p ~/.ssh" echo " chmod 700 ~/.ssh" echo " echo '${SSH_KEY}' >> ~/.ssh/authorized_keys" echo " chmod 600 ~/.ssh/authorized_keys" exit 1 fi echo "Done!"