From dd0fa10d661262e6b2f016fa5dc271e662a9dc5b Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Sun, 13 Sep 2020 22:00:33 +0200 Subject: + remote-support.sh --- scripts/remote-support.sh | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100755 scripts/remote-support.sh diff --git a/scripts/remote-support.sh b/scripts/remote-support.sh new file mode 100755 index 0000000..528ea27 --- /dev/null +++ b/scripts/remote-support.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +# Remote access + +DEPENDENCIES=("sshd" "systemctl" "ssh" "ssh-keygen" "sudo") +SSH_ACCESS_PUBKEY="" +REMOTE_ADDRESS="" + +for dep in ${DEPENDENCIES[@]}; do + which $dep > /dev/null || exit 1 +done + +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 --- + cat $HOME/.ssh/id_rsa_remote.pub + echo --- + + read -p "Press ENTER to continue." +fi + +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. + +[ -z "$KEEP_KEY" ] && sed -i "\|^$SSH_ACCESS_PUBKEY$|d" \ + "$HOME/.ssh/authorized_keys" -- cgit v1.2.3