Skip to content

Instantly share code, notes, and snippets.

@stralex7
Created February 7, 2019 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stralex7/9a29ca61933bdc94327a85b866327587 to your computer and use it in GitHub Desktop.
Save stralex7/9a29ca61933bdc94327a85b866327587 to your computer and use it in GitHub Desktop.

The output below is from a host behind a firewall

user@rig04:~$ cat reverse_ssh.sh

#!/bin/bash

COMMAND="ssh -p8022 -N -f -R 0.0.0.0:18022:localhost:22 user1@host1
pgrep -f -x "$COMMAND" > /dev/null 2>&1 || $COMMAND

user1/host1 are in host accessible from the internet, public key authenticaion.

user@rig04:~$ crontab -l
# m h  dom mon dow   command
*/5 * * * * /home/user/reverse_ssh.sh

Also it is a good idea to configure keepalives, as NAT sessions tend to sleep on home routers.

user@rig04:~$ cat .ssh/config
ServerAliveInterval 10
ExitOnForwardFailure yes

At the end use ssh -p18022 user@localhost from your host on the internet.

Cheers!

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