Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # /etc/hosts format
- # 127.0.0.1 www.google.com
- import os
- import re
- import shutil
- import socket
- import sqlite3
- import urllib.parse
- # For easy portability...
- BASE_DIR = os.path.expanduser("~/Library/Application Support/Google/Chrome/Default/")
- BOOKMARKS_FILENAME = "Bookmarks"
- HISTORY_FILENAME = "History"
- ALLOWED_SCHEMAS = ["http", "https", "ftp"]
- IGNORED_HOSTS = ["localhost"]
- HOSTS = set()
- # If possible, set the socket timeout, in case of DNS problems
- if hasattr(socket, 'setdefaulttimeout'):
- socket.setdefaulttimeout(2)
- # Some functions
- def get_ip(hostname):
- return socket.gethostbyname(hostname)
- def get_bookmarks():
- try:
- bookmarks = open(BASE_DIR + BOOKMAKRS_FILENAME).read()
- except:
- return
- for url in re.findall('"url": "(.*?)"', bookmarks):
- try:
- url = urllib.parse.urlsplit(url)
- except:
- continue
- else:
- if url.scheme in ALLOWED_SCHEMAS and ".".join(url.hostname.split(".")[-2:]) not in IGNORED_HOSTS:
- HOSTS.add(url.hostname)
- def get_history():
- history_file = BASE_DIR + HISTORY_FILENAME
- history_tmp = os.path.expanduser("~/.chrome.history.sqlite3")
- # Make a temporary copy of the file
- shutil.copy(history_file, history_tmp)
- try:
- con = sqlite3.connect(history_tmp)
- urls = con.execute('select distinct url from urls').fetchall()
- except:
- return
- for url in urls:
- try:
- url = urllib.parse.urlsplit(url[0])
- except:
- continue
- else:
- if url.scheme in ALLOWED_SCHEMAS and ".".join(url.hostname.split(".")[-2:]) not in IGNORED_HOSTS:
- HOSTS.add(url.hostname)
- con.close()
- os.remove(history_tmp)
- # Gather urls
- get_bookmarks()
- get_history()
- # Cycle through everything and gather ips
- if HOSTS:
- # Sort hostnames by domain name
- HOSTS = list(HOSTS)
- HOSTS.sort(key=lambda x: ".".join(x.split(".")[-2:]))
- for hostname in HOSTS:
- # Make sure the hostname isn't an IP address
- if re.findall('^\d+\.\d+\.\d+\.\d+$', hostname):
- continue
- # Make sure the hostname isn't a bonjour address
- if hostname.endswith(".local"):
- continue
- try:
- ip = get_ip(hostname)
- except:
- continue
- print("%s\t%s" % (ip, hostname))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement