Is there a script to add port forwarding rule in home router?

15

7

TL;DR: I am looking for a script or a cron job, which will periodically run on a linux host (fedora on raspberry pi) which will check if a port forwarding rule still exists in the router and add it if it isn't. The aim is to always have access to the raspberry pi linux host via SSH, VNC and the transmission web interface, from any machine on the internet outside my home network. The setup is given below:

Router:

Beetel 440Tx1 ADSL2 router+modem+wifi.

Setup:

Router is connected to the internet (ISP Broadband) and has a dynamic external IP. It provides a private home network to my computers with internal IPs 192.168.x.y acting as a DHCP too.

Host:

A raspberry pi ARM host with Fedora Linux, running all the time with ssh, vnc, transmission-daemon servers started at boot time. It also has the no-ip.com dyndns free DUC (dynamic update client) which routinely checks the external IP and binds it to host string. So I can always find my router's external IP by resolving my dyndns string like myrouter.no-ip.org. The pi has a static internal IP like 192.168.1.z.

Port Forwarding:

Have to login to router using GUI/browser with factory credentials only if connected to that network either through ethernet cable or password protected wi-fi

http://192.168.1.1/html/index1.html

I setup the rule by logging in to forward any traffic at ports 22, 5900, 9091 at the external IP to the respective listener programs (sshd, vncserver, transmission-daemon) on the pi at 192.168.1.z.

Problem:

This router loses the above port forwarding rule when it (the router) is restarted, or even if there is a spike in electric power and the UPS has to momentarily step in, and usually gets a different external IP from my ISP dynamically.

Requirement:

A script or cron job that can run on my fedora linux pi that can login to my router and periodically poll for the existence of that port forwarding rule and create it if it is missing. Help appreciated.

PKM

Posted 2013-08-21T07:59:23.150

Reputation: 493

1Does the router support setting up port forwarding via UPnP? (can programs running in windows/mac/linux automatically set up port forwarding rules themselves currently?) – Scott Chamberlain – 2013-08-21T08:40:45.373

I think so because http://www.pcwintech.com/showimage?file=files/screenshots/beetel-440txi/024.png

I don't really know much about UPnP other than the wikipedia page. Googling for that router type (in question) led me to that page.

– PKM – 2013-08-21T08:43:56.917

Answers

18

MiniUPnP is a command line UPnP client that allows you to enable port forwarding. The source is available so you should be able to compile it to the Pi.

I think this is the syntax you need, but I don't have a machine to test it on. You would just put the following in a script that would set up the uPnP ports for you

upnpc -a `ifconfig wlan0 | grep "inet addr" | cut -d : -f 2 | cut -d " " -f 1` 22 22 TCP
upnpc -a `ifconfig wlan0 | grep "inet addr" | cut -d : -f 2 | cut -d " " -f 1` 5900 5900 TCP
upnpc -a `ifconfig wlan0 | grep "inet addr" | cut -d : -f 2 | cut -d " " -f 1` 9091 9091 TCP

Scott Chamberlain

Posted 2013-08-21T07:59:23.150

Reputation: 28 923

that works, however what is the right place to add it so that the command runs on startup ? – Ciasto piekarz – 2016-09-03T09:14:20.100

1You can use @reboot <cmd> in your crontab – Martin – 2016-12-20T19:25:37.427

1miniupnpc should be available with raspbian with apt – Scott – 2017-09-22T01:14:50.220

and upnpc, if run from the machine running the service, will use the local address so the ifconfig/grep is unnecessary – Scott – 2017-09-22T01:16:03.170

the ifconfig ... part didn't work for me so I used ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p' as per this answer.

– Helder S Ribeiro – 2018-03-13T20:13:38.320