Advertisement
guyfox

chrome-dns-lookup

Mar 28th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # /etc/hosts format
  4. # 127.0.0.1 www.google.com
  5.  
  6. import os
  7. import re
  8. import shutil
  9. import socket
  10. import sqlite3
  11. import urllib.parse
  12.  
  13. # For easy portability...
  14. BASE_DIR = os.path.expanduser("~/Library/Application Support/Google/Chrome/Default/")
  15. BOOKMARKS_FILENAME = "Bookmarks"
  16. HISTORY_FILENAME = "History"
  17. ALLOWED_SCHEMAS = ["http", "https", "ftp"]
  18. IGNORED_HOSTS = ["localhost"]
  19. HOSTS = set()
  20.  
  21. # If possible, set the socket timeout, in case of DNS problems
  22. if hasattr(socket, 'setdefaulttimeout'):
  23.     socket.setdefaulttimeout(2)
  24.  
  25. # Some functions
  26. def get_ip(hostname):
  27.     return socket.gethostbyname(hostname)
  28.  
  29. def get_bookmarks():
  30.     try:
  31.         bookmarks = open(BASE_DIR + BOOKMAKRS_FILENAME).read()
  32.     except:
  33.         return
  34.     for url in re.findall('"url": "(.*?)"', bookmarks):
  35.         try:
  36.             url = urllib.parse.urlsplit(url)
  37.         except:
  38.             continue
  39.         else:
  40.             if url.scheme in ALLOWED_SCHEMAS and ".".join(url.hostname.split(".")[-2:]) not in IGNORED_HOSTS:
  41.                 HOSTS.add(url.hostname)
  42.  
  43. def get_history():
  44.     history_file = BASE_DIR + HISTORY_FILENAME
  45.     history_tmp = os.path.expanduser("~/.chrome.history.sqlite3")
  46.     # Make a temporary copy of the file
  47.     shutil.copy(history_file, history_tmp)
  48.     try:
  49.         con = sqlite3.connect(history_tmp)
  50.         urls = con.execute('select distinct url from urls').fetchall()
  51.     except:
  52.         return
  53.     for url in urls:
  54.         try:
  55.             url = urllib.parse.urlsplit(url[0])
  56.         except:
  57.             continue
  58.         else:
  59.             if url.scheme in ALLOWED_SCHEMAS and ".".join(url.hostname.split(".")[-2:]) not in IGNORED_HOSTS:
  60.                 HOSTS.add(url.hostname)
  61.     con.close()
  62.     os.remove(history_tmp)
  63.  
  64. # Gather urls
  65. get_bookmarks()
  66. get_history()
  67.  
  68. # Cycle through everything and gather ips
  69. if HOSTS:
  70.     # Sort hostnames by domain name
  71.     HOSTS = list(HOSTS)
  72.     HOSTS.sort(key=lambda x: ".".join(x.split(".")[-2:]))
  73.    
  74.     for hostname in HOSTS:
  75.         # Make sure the hostname isn't an IP address
  76.         if re.findall('^\d+\.\d+\.\d+\.\d+$', hostname):
  77.             continue
  78.         # Make sure the hostname isn't a bonjour address
  79.         if hostname.endswith(".local"):
  80.             continue
  81.         try:
  82.             ip = get_ip(hostname)
  83.         except:
  84.             continue
  85.         print("%s\t%s" % (ip, hostname))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement