Skip to content

Commit

Permalink
add timestamp to output file
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Dec 31, 2025
1 parent ab2e832 commit 3eab1f5
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions local-app/python-tools/test-cross-organization/test-cross-org.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
#!/bin/env python

import boto3
import argparse
import os
import sys
import logging
import time
from datetime import datetime
from pathlib import Path
from botocore.exceptions import ClientError, ProfileNotFound

# --- VERSIONING ---
__version__ = "1.0.19"
__version__ = "1.0.20"

def get_args():
parser = argparse.ArgumentParser(description=f"AWS Org Connectivity Test v{__version__}")
parser.add_argument("--profile", default=os.environ.get("AWS_PROFILE"), help="AWS CLI profile")
parser.add_argument("--region", default=os.environ.get("AWS_DEFAULT_REGION", "us-east-1"), help="AWS Region")
parser.add_argument("--role-name", required=True, help="Role name to assume in member accounts")
parser.add_argument("--sort", choices=['id', 'name'], default='name', help="Field to sort results by")
parser.add_argument("--output", help="Optional filename to save the results to at the end")
parser.add_argument("--output", help="Optional filename to save the results (will auto-append timestamp)")
parser.add_argument("--debug", action="store_true", help="Enable verbose wire-trace logging")
return parser.parse_args()

Expand All @@ -41,12 +45,9 @@ def get_full_path(org_client, entity_id, cache):

def test_connectivity(args):
start_time = time.perf_counter()

# List to capture all lines for final file dump
output_log = []

def log_print(message=""):
"""Prints to console and appends to the output_log list."""
print(message)
output_log.append(message)

Expand All @@ -61,6 +62,14 @@ def log_print(message=""):
caller = sts_client.get_caller_identity()
partition = caller['Arn'].split(':')[1]

# Determine output filename with timestamp if requested
final_filename = None
if args.output:
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
p = Path(args.output)
# Insert timestamp before the extension (e.g., results.txt -> results_20251231_101500.txt)
final_filename = f"{p.stem}_{ts}{p.suffix}"

log_print("-" * 100)
log_print(f"AWS ORG CONNECTIVITY TEST - Version {__version__}")
log_print("-" * 100)
Expand All @@ -70,8 +79,8 @@ def log_print(message=""):
log_print(f" Source Identity: {caller['Arn']}")
log_print(f" Base Region: {session.region_name}")
log_print(f" Target Role: {args.role_name}")
if args.output:
log_print(f" Output File: {args.output}")
if final_filename:
log_print(f" Output File: {final_filename}")
log_print("-" * 100)

log_print("Gathering Organization accounts...")
Expand Down Expand Up @@ -135,12 +144,11 @@ def log_print(message=""):
log_print(f"Total Elapsed Time: {minutes}m {seconds}s | Total Accounts: {len(all_accounts)}")
log_print("=" * 85)

# FINAL FILE DUMP
if args.output:
if final_filename:
try:
with open(args.output, 'w') as f:
with open(final_filename, 'w') as f:
f.write("\n".join(output_log))
print(f"\nDone. Results saved to {args.output}")
print(f"\nDone. Results saved to {final_filename}")
except Exception as e:
print(f"Warning: Could not save output file: {e}")

Expand Down

0 comments on commit 3eab1f5

Please sign in to comment.