Skip to content

Instantly share code, notes, and snippets.

@M1ke
Last active December 24, 2015 13:49
Show Gist options
  • Save M1ke/6808211 to your computer and use it in GitHub Desktop.
Save M1ke/6808211 to your computer and use it in GitHub Desktop.
A set of scripts to enable remote SSH tunnelling on a device. Originally written for a Raspberry Pi, so that when installed in a remote location inside a network it can easily be accessed for maintenance.
#!/bin/bash
# This sits on the device you want to be able to tunnel back into
# Replace USER and SERVER lines with your tunnel user (if different from local) and domain or IP
createTunnel() {
/usr/bin/ssh -N -R 2222:localhost:22 USER@SERVER
if [[ $? -eq 0 ]]; then
echo Tunnel created successfully
else
echo An error occurred creating a tunnel. RC was $?
fi
}
/bin/pidof ssh
if [[ $? -ne 0 ]]; then
echo Creating new tunnel connection
createTunnel
fi
# To go into the crontab of the device
*/1 * * * * /path/to/script/create-tunnel.sh >> /path/to/script/create-tunnel.log 2>&1
#!/bin/bash
# Assuming ports remain unchanged, run this on your tunnel server to connect back in to your device.
# To test the connection run `sudo lsof -i -n | egrep '\<sshd\>'` taken from http://superuser.com/a/248391/85813
user=$1
ssh -l $user -p 2222 localhost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment