Last active
November 11, 2025 15:01
-
-
Save rotarydrone/7dd063274495a44d0432cfce39acd9d1 to your computer and use it in GitHub Desktop.
NXC Spider2CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| ''' | |
| Converts nxc file share spider_plus json output to csv, for better grepping. | |
| ''' | |
| import json | |
| import csv | |
| import os | |
| import argparse | |
| import sys | |
| import glob | |
| def get_server_name(filename): | |
| # Extract server name from the json filename | |
| # Assuming filename format like 'jup-fs.jupiter.lab.json' | |
| try: | |
| base_name = os.path.basename(filename) | |
| # Remove .json extension and split by dots | |
| name_parts = base_name.replace('.json', '').split('.') | |
| # The server name is the first part before the first dot | |
| server_name = name_parts[0] | |
| return server_name | |
| except IndexError: | |
| return "unknown" | |
| def get_json_files_from_directory(directory_path): | |
| """Get all JSON files from the specified directory (non-recursive)""" | |
| if not os.path.isdir(directory_path): | |
| print(f"Error: Directory '{directory_path}' not found", file=sys.stderr) | |
| sys.exit(1) | |
| json_files = glob.glob(os.path.join(directory_path, "*.json")) | |
| if not json_files: | |
| print(f"Error: No JSON files found in directory '{directory_path}'", file=sys.stderr) | |
| sys.exit(1) | |
| return json_files | |
| def parse_json_to_smb(input_file, output_file): | |
| # Read JSON file | |
| try: | |
| with open(input_file, 'r') as f: | |
| data = json.load(f) | |
| except FileNotFoundError: | |
| print(f"Error: Input file '{input_file}' not found", file=sys.stderr) | |
| sys.exit(1) | |
| except json.JSONDecodeError: | |
| print(f"Error: Invalid JSON in file '{input_file}'", file=sys.stderr) | |
| sys.exit(1) | |
| # Get server name | |
| server_name = get_server_name(input_file) | |
| # Prepare CSV output | |
| try: | |
| with open(output_file, 'w', newline='') as f: | |
| writer = csv.writer(f) | |
| # Write header | |
| writer.writerow(['SMB_Path', 'Size', 'Last_Modified']) | |
| # Process each share and its contents | |
| for share, contents in data.items(): | |
| for filepath, details in contents.items(): | |
| # Construct SMB path | |
| smb_path = f"\\\\{server_name}\\{share}\\{filepath}" | |
| # Replace forward slashes with backslashes | |
| smb_path = smb_path.replace('/', '\\') | |
| # Write row with path and details | |
| writer.writerow([ | |
| smb_path, | |
| details.get('size', ''), | |
| details.get('mtime_epoch', '') | |
| ]) | |
| except PermissionError: | |
| print(f"Error: Permission denied when writing to '{output_file}'", file=sys.stderr) | |
| sys.exit(1) | |
| def main(): | |
| parser = argparse.ArgumentParser( | |
| description='Convert JSON file system structure to SMB share paths CSV', | |
| formatter_class=argparse.RawDescriptionHelpFormatter, | |
| epilog=''' | |
| Example: | |
| %(prog)s -i /path/to/input.json -o output.csv | |
| %(prog)s -d /path/to/json/files/ -o output.csv | |
| The input JSON should contain file system structure information. | |
| The output CSV will contain SMB paths and file details.''') | |
| parser.add_argument('-i', '--input', | |
| help='Input JSON file path') | |
| parser.add_argument('-d', '--directory', | |
| help='Directory containing JSON files to process') | |
| parser.add_argument('-o', '--output', | |
| required=True, | |
| help='Output CSV file path') | |
| parser.add_argument('-v', '--verbose', | |
| action='store_true', | |
| help='Enable verbose output') | |
| args = parser.parse_args() | |
| # Validate that either input file or directory is provided | |
| if not args.input and not args.directory: | |
| parser.error("Either --input or --directory must be specified") | |
| if args.input and args.directory: | |
| parser.error("Cannot specify both --input and --directory") | |
| # Determine files to process | |
| if args.directory: | |
| json_files = get_json_files_from_directory(args.directory) | |
| if args.verbose: | |
| print(f"Found {len(json_files)} JSON files in directory {args.directory}") | |
| else: | |
| json_files = [args.input] | |
| if args.verbose: | |
| print(f"Output will be written to {args.output}") | |
| # Process files and combine output | |
| try: | |
| with open(args.output, 'w', newline='') as f: | |
| writer = csv.writer(f) | |
| # Write header | |
| writer.writerow(['SMB_Path', 'Size', 'Last_Modified']) | |
| for json_file in json_files: | |
| if args.verbose: | |
| print(f"Processing {json_file}") | |
| # Read JSON file | |
| try: | |
| with open(json_file, 'r') as jf: | |
| data = json.load(jf) | |
| except FileNotFoundError: | |
| print(f"Error: Input file '{json_file}' not found", file=sys.stderr) | |
| continue | |
| except json.JSONDecodeError: | |
| print(f"Error: Invalid JSON in file '{json_file}'", file=sys.stderr) | |
| continue | |
| # Get server name | |
| server_name = get_server_name(json_file) | |
| # Process each share and its contents | |
| for share, contents in data.items(): | |
| for filepath, details in contents.items(): | |
| # Construct SMB path | |
| smb_path = f"\\\\{server_name}\\{share}\\{filepath}" | |
| # Replace forward slashes with backslashes | |
| smb_path = smb_path.replace('/', '\\') | |
| # Write row with path and details | |
| writer.writerow([ | |
| smb_path, | |
| details.get('size', ''), | |
| details.get('mtime_epoch', '') | |
| ]) | |
| except PermissionError: | |
| print(f"Error: Permission denied when writing to '{args.output}'", file=sys.stderr) | |
| sys.exit(1) | |
| if args.verbose: | |
| print("Conversion completed successfully") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment