Skip to content

Instantly share code, notes, and snippets.

@rotarydrone
Created February 19, 2025 00:20
Show Gist options
  • Save rotarydrone/7dd063274495a44d0432cfce39acd9d1 to your computer and use it in GitHub Desktop.
Save rotarydrone/7dd063274495a44d0432cfce39acd9d1 to your computer and use it in GitHub Desktop.
NXC Spider2CSV
#!/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
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)
server_name = base_name.split('.')[1] + '.' + base_name.split('.')[2]
return server_name
except IndexError:
return "unknown"
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
The input JSON should contain file system structure information.
The output CSV will contain SMB paths and file details.''')
parser.add_argument('-i', '--input',
required=True,
help='Input JSON file path')
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()
if args.verbose:
print(f"Processing {args.input}")
print(f"Output will be written to {args.output}")
parse_json_to_smb(args.input, args.output)
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