Files
dotfiles/.local/bin/station-setup-server

41 lines
1.5 KiB
Plaintext
Raw Permalink Normal View History

2026-03-14 16:07:57 +00:00
#! /bin/bash
echo "-------------------------------------"
echo "Server Setup Script"
echo "-------------------------------------"
echo "Extracting login details from enpass"
2026-03-14 19:40:46 +00:00
SERVER=$(enpass-cli -vault="$HOME/.local/share/Enpass/Enpass/Vaults/primary" -json -sort show "Scarif space" | jq '.[] | select(.label=="New new admin password")')
SERVERUN=$(echo "$SERVER" | jq -r '.login')
SERVERPW=$(echo "$SERVER" | jq -r '.password')
2026-03-14 16:07:57 +00:00
SSHPATH="$HOME/.ssh/id_ed25519" # The path to the SSH key file
2026-03-14 19:15:38 +00:00
SERVERIP="scarif.space"
SERVERPORT=629
2026-03-14 16:07:57 +00:00
2026-03-14 19:15:38 +00:00
echo "~~~~~~~~"
2026-03-14 16:07:57 +00:00
echo "Generating the SSH key if it does not exist"
[ ! -f $SSHPATH ] && ssh-keygen -t ed25519 -f "$SSHPATH" -N "" -q
KEY=$(cat "$SSHPATH.pub")
echo "~~~~~~~~"
2026-03-14 19:15:38 +00:00
echo "Copying SSH key to server known hosts"
2026-03-14 19:40:46 +00:00
ssh-keygen -F $SERVERIP >/dev/null || ssh-keyscan -p $SERVERPORT $SERVERIP >> ~/.ssh/known_hosts
2026-03-14 16:07:57 +00:00
2026-03-14 19:15:38 +00:00
echo "~~~~~~~~"
echo "Checking if key exists on the server"
if sshpass -p "$SERVERPW" ssh -o PasswordAuthentication=yes -p $SERVERPORT "$SERVERUN@$SERVERIP" "grep -q \"$KEY\" ~/.ssh/authorized_keys 2>/dev/null"; then
echo "Key already exists on the server. Skipping upload."
exit 0
2026-03-14 16:07:57 +00:00
fi
2026-03-14 19:15:38 +00:00
echo "~~~~~~~~"
echo "Uploading the SSH key to the server"
sshpass -p "$SERVERPW" ssh -p $SERVERPORT "$SERVERUN@$SERVERIP" "mkdir -p ~/.ssh && echo '$KEY' >> ~/.ssh/authorized_keys && chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys"
2026-03-14 16:07:57 +00:00
2026-03-14 19:15:38 +00:00
echo "~~~~~~~~"
echo "Testing SSH connection"
ssh -i "$SSHPATH" -p $SERVERPORT "$SERVERUN@$SERVERIP" "echo 'Successfully connected to $SERVERIP'"
2026-03-14 16:07:57 +00:00