shell script to abtain wan ip from genie

1 post / 0 new
replabrobin
replabrobin's picture
shell script to abtain wan ip from genie

The bash script below uses python2 to obtain my wan ip address from my genie router.

You'll need save the text and chmod a+x the script file to make it executable.

Edit to change location of your python 2.x, user name, password and the ip address wher your router sits.  The double hit is to fix errors where the user/password dialog sometimes fails (for me at least).

 

 

#!/bin/bash
#edit to suit your system
PYTHON="/bin/python2"
ip=$($PYTHON << END
import urllib, time
#edit these to suit your system
u,p,h='admin','password','192.168.0.1'
url='http://%s:%[email protected]%s/ADV_home2.htm' % (u,p,h)
class MyURLOpener(urllib.FancyURLopener):
    def prompt_user_passwd(self, host, realm):
        return tuple(u,p)
urllib._urlopener=MyURLOpener()
try:
    html=urllib.urlopen(url).read()
except EOFError:
    time.sleep(1)
    try:
        html=urllib.urlopen(url).read()
    except:
        print ''
print html.split('>IP Address </td>',2)[2].split('>',1)[1].split('<')[0]
END
) 2> /dev/null
[ -z "$ip" ] && echo "!!!!! can't get wan ip" 1>&2 && exit 1
echo "$ip"