aboutsummaryrefslogtreecommitdiff
path: root/scripts/remote-support.sh
blob: 40d36429ea2f8c109bdc569b39703e96171725d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash

# Remote access
#
# Forward local SSH server to remote host to make it accessible
# remotely without NAT/Firewall mapping

DEPENDENCIES=("sshd" "systemctl" "ssh" "ssh-keygen" "sudo")
SSH_ACCESS_PUBKEY=""
REMOTE_ADDRESS=""

for dep in ${DEPENDENCIES[@]}; do
	which $dep > /dev/null || exit 1
done

read -p "Connecting to $REMOTE_ADDRESS. Proceed? (y/[n]) > " proceed
case $proceed in
	[yY]* )
		;;
	* )
		echo Aborting.
		exit 1
esac

PORT=$(( ($RANDOM % 64000) + 1024 ))

trap 'kill -s SIGKILL $SSH_PID' 1 2 9

if [ ! -f $HOME/.ssh/id_rsa_remote ]; then
	echo No SSH Key found. Creating one.

	ssh-keygen -t rsa -N "" -C "remote key $USER@$HOSTNAME" \
		-f "$HOME/.ssh/id_rsa_remote"

	echo --- $HOME/.ssh/id_rsa_remote.pub ---
	cat $HOME/.ssh/id_rsa_remote.pub
	echo --- END ---
	
	read -p "Press ENTER to continue."
fi

# Check if key is already allowed to connect, add if not
grep -q "^$SSH_ACCESS_PUBKEY$" "$HOME/.ssh/authorized_keys" && \
	KEEP_KEY="YES" || \
	echo "$SSH_ACCESS_PUBKEY" >> "$HOME/.ssh/authorized_keys"

sudo systemctl start sshd || echo "Failed to start sshd."

ssh -R -N 22:$REMOTE_ADDRESS:$PORT &
SSH_PID=$!

echo Connected to $REMOTE_ADDRESS
echo "=> U: $USER P: $PORT"
echo CTRL+C to disconnect

while kill -s 0 $PID; do sleep 1; done

echo Connection closed.

# Remove Key from authorized_keys if it wasn't originally there
[ -z "$KEEP_KEY" ] && sed -i "\|^$SSH_ACCESS_PUBKEY$|d" \
	"$HOME/.ssh/authorized_keys"