Skip to content

Instantly share code, notes, and snippets.

@benfulton
Created January 16, 2014 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benfulton/8449705 to your computer and use it in GitHub Desktop.
Save benfulton/8449705 to your computer and use it in GitHub Desktop.
Copy registered machines from a LinkSys router to a hosts file, by scraping a router web page
import urllib2
import base64
import re
request = urllib2.Request("http://192.168.1.1/DHCPTable.asp")
base64string = base64.encodestring('%s:%s' % (USER, PASSWORD)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
lines = (re.findall(r"'(.*?)'", x, re.DOTALL) for x in urllib2.urlopen(request) if 'new AAA' in x)
q = dict((vv[0], vv[1]) for vv in lines if vv)
def is_match(line, q):
for host in q.values():
if line.startswith(host):
return True
return False
with open('C:\WINDOWS\system32\drivers\etc\hosts') as f:
host_lines = [line.strip() for line in f if not is_match(line, q)]
host_lines += ["%s %s" % (v,k) for k,v in q.iteritems()]
for vvv in host_lines:
print vvv
with open('C:\WINDOWS\system32\drivers\etc\hosts', 'w') as fw:
for line in host_lines:
fw.write(line + "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment