Skip to content

Instantly share code, notes, and snippets.

@mvadu
Created March 30, 2019 02:13
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mvadu/44c55b05d4614cf07d9833e0b44bf27a to your computer and use it in GitHub Desktop.
Save mvadu/44c55b05d4614cf07d9833e0b44bf27a to your computer and use it in GitHub Desktop.
THis script uses upnp client (upnpc) to talk to router and open ports
#!/bin/bash
#~/bin/upnpPortMapper.sh
#sudo apt-get install miniupnpc
#crontab -l | grep upnp || echo $(crontab -l ; echo '*/5 * * * * ~/bin/upnpPortMapper.sh >/dev/null 2>&1') | crontab -
export LC_ALL=C
router=$(ip r | grep default | cut -d " " -f 3)
gateway=$(upnpc -l | grep "desc: http://$router:[0-9]*/rootDesc.xml" | cut -d " " -f 3)
ip=$(upnpc -l | grep "Local LAN ip address" | cut -d: -f2)
external=80
port=80
upnpc -u $gateway -d $external TCP
upnpc -u $gateway -e "Web mapping for RaspberryPi" -a $ip $port $external TCP
external=443
port=443
upnpc -u $gateway -d $external TCP
upnpc -u $gateway -d $external UDP
upnpc -u $gateway -e "TLS Web mapping for RaspberryPi" -a $ip $port $external TCP
@pavo325
Copy link

pavo325 commented May 7, 2019

This is awesome, I've been try work this out for days, ive added it to my domoticz.sh file and it works a treat. Many Thanks mvadu.

@TekExplorer
Copy link

Router and gateway dont seem to be needed. Still, a useful gist.

@mvadu
Copy link
Author

mvadu commented Jul 5, 2021

Router and gateway dont seem to be needed. Still, a useful gist.

You would need them if you happen to have more than one UPNP servers which broadcasts own self. In that case script checks where it got the IP, and generates a upnp gateway url.

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