Skip to content

Instantly share code, notes, and snippets.

@resure
Last active August 29, 2015 13:56
Show Gist options
  • Save resure/9235295 to your computer and use it in GitHub Desktop.
Save resure/9235295 to your computer and use it in GitHub Desktop.
Debian startup file for SSH port forwaring (connecting to Raspberry Pi behing NAT)

Run update-rc.d rssh defaults after adding this file to /etc/init.d on your Raspberry.

To SSH through NAT you should use "proxy" server (TARGET in this script) with GatewayPorts yes in its /etc/ssh/sshd_config.

After configuring middle server just run ssh pi@example.com -p 2242 to connect to your Raspberry behind the NAT.

#!/bin/sh
# /etc/init.d/rssh
KEY="/home/username/.ssh/id_rsa"
PORT="2242"
TARGET="server_username@example.copm"
PIDFILE="/var/run/rssh.pid"
case "$1" in
start)
echo "Starting script rssh"
/usr/bin/ssh -i $KEY -R $PORT:localhost:22 $TARGET sleep 999999999 &
echo "$!" > $PIDFILE
;;
stop)
echo "Stopping script rssh"
PID=`cat $PIDFILE`
kill PID
;;
*)
echo "Usage: /etc/init.d/rssh {start|stop}"
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment