Skip to content

Instantly share code, notes, and snippets.

@revington
Last active March 24, 2018 18:15
Show Gist options
  • Save revington/afa730d1036677fc030e to your computer and use it in GitHub Desktop.
Save revington/afa730d1036677fc030e to your computer and use it in GitHub Desktop.
upnp set up script
#!/bin/bash
export IP=$(/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
echo "setting up ip ${IP}"
check_service(){
echo `upnpc -l | grep $1'->' | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"`
}
register(){
local service_name=$1
local port=$2
local forward_port=$3
local protocol=$4
local existing=$(check_service $forward_port)
if [ -z $existing ]
then
echo "registering $service_name"
upnpc -a ${IP} ${port} ${forward_port} ${protocol} > /dev/null
elif [ "$existing" != "$IP" ]
then
echo "unregistering $service_name from '$existing'"
upnpc -d ${forward_port} ${protocol} > /dev/null
echo 'registering transmission'
upnpc -a ${IP} ${port} ${forward_port} ${protocol} > /dev/null
else
echo "$service_name already configured"
fi
}
register ssh 22 2200 TCP
register transmision_web_ui 9091 9091 TCP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment