Last active
December 24, 2015 13:49
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# To go into the crontab of the device | |
*/1 * * * * /path/to/script/create-tunnel.sh >> /path/to/script/create-tunnel.log 2>&1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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