41 lines
1.5 KiB
Bash
Executable File
41 lines
1.5 KiB
Bash
Executable File
#! /bin/bash
|
|
|
|
echo "-------------------------------------"
|
|
echo "Server Setup Script"
|
|
echo "-------------------------------------"
|
|
|
|
echo "Extracting login details from enpass"
|
|
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')
|
|
|
|
SSHPATH="$HOME/.ssh/id_ed25519" # The path to the SSH key file
|
|
SERVERIP="scarif.space"
|
|
SERVERPORT=629
|
|
|
|
echo "~~~~~~~~"
|
|
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 "~~~~~~~~"
|
|
echo "Copying SSH key to server known hosts"
|
|
ssh-keygen -F $SERVERIP >/dev/null || ssh-keyscan -p $SERVERPORT $SERVERIP >> ~/.ssh/known_hosts
|
|
|
|
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
|
|
fi
|
|
|
|
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"
|
|
|
|
echo "~~~~~~~~"
|
|
echo "Testing SSH connection"
|
|
ssh -i "$SSHPATH" -p $SERVERPORT "$SERVERUN@$SERVERIP" "echo 'Successfully connected to $SERVERIP'"
|
|
|