Skip to content

Instantly share code, notes, and snippets.

@paxperscientiam
Last active October 3, 2017 15:10
Show Gist options
  • Select an option

  • Save paxperscientiam/9cba7b88c98eee645899b92a2fb05e47 to your computer and use it in GitHub Desktop.

Select an option

Save paxperscientiam/9cba7b88c98eee645899b92a2fb05e47 to your computer and use it in GitHub Desktop.
Julian Simioni's to accesss *nix machine behind home router (tweaked)
#!/bin/bash
# 'pax' is simply an alias for one my ssh connections,
# which are configured in your user ssh_config file.
# For more info, see Julian Simioni's original post:
# https://juliansimioni.com/blog/howto-access-a-linux-machine-behind-a-home-router-with-ssh-tunnels/
relay_server="pax"
createTunnel() {
/usr/bin/ssh -f -N -R 10022:localhost:22 \
-L19922:"${relay_server}":22 "${relay_server}"
if [[ $? -eq 0 ]]; then
echo Tunnel to relay server created successfully
else
echo An error occurred creating a tunnel to relay server. Return code: $?
fi
}
# works, but reproduces until saturated
#/usr/bin/ssh -p 19922 localhost ls > /dev/null
# nc seems to be working better
/usr/bin/nc -z 127.0.0.1 19922 > /dev/null
if [[ $? -ne 0 ]]; then
echo Creating new tunnel connection to relay server.
createTunnel
fi
@paxperscientiam
Copy link
Copy Markdown
Author

paxperscientiam commented Sep 28, 2017

needs to be tweaked still

EDIT: should be good to go. Be sure to check out the OP by Julian Simioni.

EDIT 2:

As of this writing, I am running this script in combination with caffeinate in the following fashion.
* * * * * /usr/bin/caffeinate -ims /Users/ramos/bin/create_tunnel

@orangejulius
Copy link
Copy Markdown

orangejulius commented Sep 29, 2017

works, but reproduces until saturated

Hmm I remember having that problem but I fixed it somehow. I'll try to remember why and i'll also publish my current version of the script soon

@paxperscientiam
Copy link
Copy Markdown
Author

Cool! I've wondered if there was some quirk to config beyond my understanding -- which is entirely probable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment