make backup path work on mac and win32unix(cygwin)
[hombase.git] / other-sh / getip1.py
blobd22d173f0e3e1fdc7f277c56cdaeeb5e025721bb
1 #!/usr/bin/python
3 import urllib2
4 import re
6 def getip():
7 theurl = 'http://192.168.1.1:8088/userRpm/StatusRpm.htm?'
8 username = 'admin'
9 password = 'admin'
10 # a great password
12 passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
13 # this creates a password manager
14 passman.add_password(None, theurl, username, password)
15 # because we have put None at the start it will always use this
16 # username/password combination for urls for which `theurl` is a super-url
18 authhandler = urllib2.HTTPBasicAuthHandler(passman)
19 # create the AuthHandler
21 opener = urllib2.build_opener(authhandler)
23 urllib2.install_opener(opener)
24 # All calls to urllib2.urlopen will now use our handler Make sure not to
25 # include the protocol in with the URL, or HTTPPasswordMgrWithDefaultRealm will
26 # be very confused. You must (of course) use it when fetching the page though.
28 try:
29 pagehandle = urllib2.urlopen(theurl)
30 except urllib2.URLError:
31 return 'No route to host'
32 # authentication is now handled automatically for us
34 thepage = pagehandle.read()
35 pagehandle.close
37 pa = re.compile(r'(var wanPara = new Array\(.*0, "[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]", ")(\d+.\d+.\d+.\d+)', re.DOTALL)
39 m = pa.search(thepage)
41 return m.group(2)
43 if __name__=='__main__':
44 print getip()