diff --git a/waccess b/waccess index 9bf701c..f4d879e 100755 --- a/waccess +++ b/waccess @@ -1,6 +1,6 @@ -#!/usr/local/bin/python +#!/usr/bin/env python # waccess - Copyright (c) 2001, TundraWare Inc., All Rights Reserved -# $Id: waccess,v 1.2 2001/08/04 22:21:49 tundra Exp $ +# $Id: waccess,v 1.3 2002/08/30 21:52:07 tundra Exp $ # # Look for selected strings passed on the command line in the http access log. @@ -24,27 +24,34 @@ # Constants ########## -ANS = ";; ANSWER SECTION" -DIG = "/usr/bin/dig -t ptr -x " -LOG = "/var/log/httpd-access.log" + +########## +# Function Defintions +########## + +def usage(): + print "usage: waccess [-rs -f logfile]" + sys.exit(2) + ########## # Command Line Processing ########## +LOG = "/var/log/httpd-access.log" REVERSE = FALSE SHOW = TRUE -START = 1 + try: - opts, args = getopt.getopt(sys.argv[1:], '-rs') + opts, args = getopt.getopt(sys.argv[1:], '-f:rs') except getopt.GetoptError: - print "usage: waccess [-rs]" - sys.exit(2) - + usage() + for opt, val in opts: - START += 1 + if opt == "-f": + LOG = val if opt == "-r": REVERSE = TRUE SHOW = TRUE @@ -65,30 +72,38 @@ total = 0 # Read in the whole log file -for records in f.read().splitlines(): +for record in f.read().splitlines(): total += 1 - # Check each command line argument for a match + # Check each log record for a match with any command line argument + + MATCHED = FALSE for a in args: - - if records.count(a): - fields = records.split() - + if record.count(a): + fields = record.split() i = 0 revname = "" - if REVERSE: - try: - revname = socket.gethostbyaddr(fields[0])[0] - except: - revname = "NO REVERSE RESOLUTION" - - if SHOW: - print fields[3][1:], " " * (19 - len(fields[3][1:])), \ - fields[0], " " * (15 - len(fields[0])), \ - revname[-(35+1):], " " * (35 - len(revname)), \ - fields[5], " " * (8 - len(fields[5])), fields[6] - matched[a] += 1 + MATCHED = TRUE + + + # But only display the matching record once, regardless of how many + # matching substrings are found. + + if MATCHED: + if REVERSE: + try: + revname = socket.gethostbyaddr(fields[0])[0] + except: + revname = "NO REVERSE RESOLUTION" + + if SHOW: + print fields[3][1:], " " * (19 - len(fields[3][1:])), \ + fields[0], " " * (15 - len(fields[0])), \ + revname[-(35+1):], " " * (35 - len(revname)), \ + fields[5], " " * (8 - len(fields[5])), fields[6] + + f.close() print "\nProcessed %d Total Records.\n" % (total,)