-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweblog_helper
More file actions
96 lines (75 loc) · 2.73 KB
/
Copy pathweblog_helper
File metadata and controls
96 lines (75 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
"""
Notes:
weblog-helper script requires netaddr python library. Please install it beforehand:
pip install netaddr
TODO:
- check if local log is present, else download from S3. Read from file
"""
import sys
if sys.version_info > (3, 0):
sys.stderr.write('This script requires Python 2.7')
sys.exit(1)
import netaddr
import argparse
import json
import logging
import requests
logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.ERROR,
datefmt='%Y-%m-%d %H:%M:%S'
)
def validate_ip(arg_source_ip):
"""Return True if *arg_source_ip* is a valid ip"""
try:
ip = netaddr.IPNetwork(arg_source_ip)
except (netaddr.core.AddrFormatError, ValueError):
raise ValueError("Argument (%s) is not in correct presentation format" % arg_source_ip)
return True
def get_access_log(target_url):
"""Return access log"""
return requests.get(target_url).content
def print_if_interesting(log_line, arg_source_ip):
"""Print *log_line* if it is interesting """
log_source_ip = get_source_ip(log_line)
if is_in_range(log_source_ip, arg_source_ip):
print(log_line)
def get_source_ip(log_line):
"""Return source ip field from *log_line* """
return log_line.split(' - - ')[0]
def is_in_range(log_source_ip, arg_source_ip):
"""Return True if *log_line* contains the source ip we are looking for """
if is_cidr:
return netaddr.IPAddress(log_source_ip) in netaddr.IPNetwork(arg_source_ip)
else:
return log_source_ip == arg_source_ip
def is_cidr(arg_source_ip):
"""Return True if provided *arg_source_ip* represents a CIDR, not a host"""
if netaddr.IPNetwork(arg_source_ip).prefixlen < 32:
return True
return False
def main():
logging.info("Execution started.")
# Read command-line parameters
parser = argparse.ArgumentParser()
parser.add_argument("--ip", action="store", required=True, dest="ip", help="Source IP or CIDR to search for.")
args = parser.parse_args()
arg_source_ip = args.ip
# Check if IP has valid format (host or cidr)
if validate_ip(arg_source_ip):
# Check if ip argument is host or cidr
# Set varible *is_cidr* as global to only check once if *arg_source_ip* is host or cidr
global is_cidr
if is_cidr(arg_source_ip):
is_cidr = True
else:
is_cidr = False
# Read http access log
# Not ideal if log file is big, would be better to use a file handler. Also downloads file every time script is run
data = get_access_log("https://s3.amazonaws.com/syseng-challenge/public_access.log.txt")
logging.info("Log file downloaded.")
for line in data.splitlines():
print_if_interesting(line, arg_source_ip)
logging.info("Execution finished.")
if __name__ == "__main__":
main()