import re
import sys
def extract_tokens(log_line):
"""Extracts authentication tokens from a log line using regex."""
# Define a regex pattern to match common token formats. Adjust as needed.
token_pattern = r"Bearer ([\w-]+)" # Example: "Bearer <token>"
match = re.search(token_pattern, log_line)
if match:
return match.group(1) # Return the token value
else:
return None
def monitor_tokens(log_file):
"""Reads a log file and extracts authentication tokens."""
try:
with open(log_file, 'r') as f:
for line in f:
token = extract_tokens(line)
if token:
print(token) # Output the token. Can be modified to store/process.
except FileNotFoundError:
print(f"Error: Log file '{log_file}' not found.")
sys.exit(1)
except Exception as e:
print(f"An error occurred: {e}")
sys.exit(1)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python token_extractor.py <log_file>")
sys.exit(1)
log_file = sys.argv[1]
monitor_tokens(log_file)
Add your comment