automatic port forwarding for the raspberry pi NAS

My pi-based NAS is working great, but I'd like it to be more robust to being moved between networks/routers. In particular I need an external port opened up for SSH. There's a nice tool for this called miniupnp which is in the raspberry pi repositories (you will have to apt-get install it).

This page describes how to automate port forwarding with upnp nicely, but since links are fragile, here's the key script:

#!/bin/bash
export LC_ALL=C

upnpc="upnpc " # extra options here
external=3333
port=22
ip=$($upnpc -l | grep "Local LAN ip address" | cut -d: -f2)

$upnpc -d $external TCP >/dev/null 2>&1
$upnpc -a $ip $port $external TCP >/dev/null 2>&1
you put this script where it will be executed any time you get a new IP from your router:
/etc/dhcp/dhclient-exit-hooks.d/
and just to be safe call it every time you reboot as well (you don't get a new IP from your router each time you reboot, so this script won't be called otherwise).
crontab -e
@reboot sleep 10 /etc/dhcp/dhclient-exit-hooks.d/port_forward
FYI not all routers support UPNP for instance ATT U-verse does not.

Comments

Email me

Name

Email *

Message *

Popular posts from this blog

Panasonic TH-42PX75U Plasma TV review: input lag and upscaling tested using the piLagTesterPRO

piLagTester PRO order page

A $5 TV Input Lag tester using a Raspberry Pi Zero