From 3eab1f5258513b32fb8dc349eefb5741fd0abbf7 Mon Sep 17 00:00:00 2001 From: badra001 Date: Wed, 31 Dec 2025 10:17:17 -0500 Subject: [PATCH] add timestamp to output file --- .../test-cross-organization/test-cross-org.py | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/local-app/python-tools/test-cross-organization/test-cross-org.py b/local-app/python-tools/test-cross-organization/test-cross-org.py index d12733b1..85c22262 100755 --- a/local-app/python-tools/test-cross-organization/test-cross-org.py +++ b/local-app/python-tools/test-cross-organization/test-cross-org.py @@ -1,13 +1,17 @@ +#!/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__}") @@ -15,7 +19,7 @@ def get_args(): 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() @@ -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) @@ -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) @@ -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...") @@ -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}")