#!/usr/bin/env python3 # # resolve_awstats.py version 1.0.1 # # Reads the AWStats hosts page and outputs a new HTML file with # IP addresses resolved via reverse lookup. # # Version history: # 1.0.0 - initial release # 1.0.1 - added beep notification when complete # # Copyright (C) 2014 by Per Ola Kristensson. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # N.B. Ensure Beautiful Soup is converted to Python 3 from bs4 import BeautifulSoup import requests import socket import re # URL to AwStats host page url = 'url' # Username user = 'username' # Password pw = 'password' re_ip_addr = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" r = requests.get(url, auth = (user, pw)) soup = BeautifulSoup(r.text) tags = soup.find_all('td', class_="aws", text=re.compile(re_ip_addr)) for t in tags: ip = t.text try: host = socket.gethostbyaddr(ip) t.string = host[0] except socket.herror: t.string = "Unresolved (" + ip + ")" print(soup, file=open('awstats_resolved.html', 'w')) print('\a')