diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2020-09-13 22:00:33 +0200 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2020-09-13 22:00:33 +0200 |
commit | dd0fa10d661262e6b2f016fa5dc271e662a9dc5b (patch) | |
tree | 0e295ea6f15eccf1e547ee3b9b252dd7b08b216e /scripts | |
parent | a79a9b617a97bfa58a066f37d3030d1e5705b0d2 (diff) | |
download | dotfiles-dd0fa10d661262e6b2f016fa5dc271e662a9dc5b.tar.gz |
+ remote-support.sh
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/remote-support.sh | 48 |
1 files changed, 48 insertions, 0 deletions
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" |