#!/bin/bash # file: /usr/bin/getownip. # permissions: chmod a+x, go-w /usr/bin/getownip # # get public IP of your router/NAT by using STUN. (c)jOERG # # usage: # getownip [ [ [ []]]] # # # you REALLY REALLY ought replace next 2 with your own provider's STUN-server! stunserver=${1:-stun.sipgate.nex} ;# You have to edit anyway, so shame on you... port=${2:-10000} ;#...if you dare to be lazy here. I'll get the flames! t_listen=${3:-2} ;# seconds to wait for answer on a single UDP-send. default: 2 num_tries=${4:-5} ;# number of UDP-sends before give up. default: 5 set -u session=`tr -d -c [:alnum:] < /dev/urandom | dd bs=16 count=1 2>/dev/null` for (( ; num_tries > 0; num_tries-- )) \ do ip=`(echo -ne '\x00\x01\x00\x08'; \ echo -n "$session"; \ echo -ne '\x00\x03\x00\x04\x00\x00\x00\x00'\ ) \ | netcat -w ${t_listen} -u ${stunserver} ${port} \ | od -w4 -t u1 -j 28 -N 4 -A n \ | sed 's/^ *//g;s/\ */./g'` if [ "$ip" ] then echo $ip exit 0 else echo -n "." >&2 fi done exit 3