Created
June 28, 2025 02:27
-
-
Save lucabased/4c8ca3d3395890ee9af9c1cd6aa10442 to your computer and use it in GitHub Desktop.
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
| import redis | |
| import os | |
| from urllib.parse import urlparse | |
| from time import time | |
| from concurrent.futures import ThreadPoolExecutor, as_completed | |
| # For colored output | |
| try: | |
| from colorama import init, Fore, Style | |
| init(autoreset=True) | |
| COLORAMA = True | |
| except ImportError: | |
| COLORAMA = False | |
| print("[INFO] Install 'colorama' for colored output: pip install colorama") | |
| # Configuration | |
| REDIS_URL = os.getenv('REDIS_URL', 'CHANGEME') # <==== CHANGE THIS!! | |
| url = urlparse(REDIS_URL) | |
| host = url.hostname | |
| port = url.port or 6379 | |
| db = int(url.path[1:]) if url.path[1:] else 0 | |
| print((Fore.CYAN if COLORAMA else '') + f"Connecting to Redis at {host}:{port}, DB: {db}" + (Style.RESET_ALL if COLORAMA else '')) | |
| # Connect to Redis | |
| r = redis.Redis(host=host, port=port, db=db, password=url.password) | |
| try: | |
| r.ping() | |
| print((Fore.GREEN if COLORAMA else '') + "Successfully connected to Redis." + (Style.RESET_ALL if COLORAMA else '')) | |
| except Exception as e: | |
| print((Fore.RED if COLORAMA else '') + f"Failed to connect to Redis: {e}" + (Style.RESET_ALL if COLORAMA else '')) | |
| exit(1) | |
| # Keyword to search for | |
| KEYWORD = input((Fore.YELLOW if COLORAMA else '') + 'Enter keyword to search for: ' + (Style.RESET_ALL if COLORAMA else '')) | |
| print((Fore.CYAN if COLORAMA else '') + f"Searching for keys containing: '{KEYWORD}'" + (Style.RESET_ALL if COLORAMA else '')) | |
| BATCH_SIZE = 10000 # Number of keys to fetch per pipeline batch | |
| SCAN_COUNT = 10000 # Number of keys to scan per iteration | |
| MAX_WORKERS = 8 # Number of threads for parallel value fetching | |
| output_file = 'results.txt' | |
| def fetch_values(batch, redis_url): | |
| # Each thread creates its own connection | |
| url = urlparse(redis_url) | |
| rlocal = redis.Redis(host=url.hostname, port=url.port or 6379, db=int(url.path[1:]) if url.path[1:] else 0, password=url.password) | |
| pipe = rlocal.pipeline() | |
| for k in batch: | |
| pipe.get(k) | |
| values = pipe.execute() | |
| results = [] | |
| for k, v in zip(batch, values): | |
| results.append((k, v)) | |
| return results | |
| found = False | |
| count = 0 | |
| start_time = time() | |
| with open(output_file, 'w', encoding='utf-8') as f: | |
| keys_batch = [] | |
| index = 1 | |
| cursor = 0 | |
| futures = [] | |
| with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor: | |
| while True: | |
| cursor, keys = r.scan(cursor=cursor, match=f'*{KEYWORD}*', count=SCAN_COUNT) | |
| keys_batch.extend(keys) | |
| while len(keys_batch) >= BATCH_SIZE: | |
| batch = keys_batch[:BATCH_SIZE] | |
| keys_batch = keys_batch[BATCH_SIZE:] | |
| futures.append(executor.submit(fetch_values, batch, REDIS_URL)) | |
| if cursor == 0: | |
| break | |
| # Process any remaining keys | |
| if keys_batch: | |
| futures.append(executor.submit(fetch_values, keys_batch, REDIS_URL)) | |
| for future in as_completed(futures): | |
| results = future.result() | |
| for k, v in results: | |
| line = f"[{index}] Key: {k.decode('utf-8')} => Value: {(v.decode('utf-8', errors='replace') if v else '[None]')}\n" | |
| f.write(line) | |
| print((Fore.MAGENTA if COLORAMA else '') + f"[{index}] Key: " + (Fore.YELLOW if COLORAMA else '') + f"{k.decode('utf-8')}" + (Fore.MAGENTA if COLORAMA else '') + " => " + (Fore.GREEN if COLORAMA else '') + f"{(v.decode('utf-8', errors='replace') if v else '[None]')}" + (Style.RESET_ALL if COLORAMA else '')) | |
| found = True | |
| count += 1 | |
| index += 1 | |
| if count % 10000 == 0 and count > 0: | |
| print((Fore.CYAN if COLORAMA else '') + f"Scanned {count} keys so far..." + (Style.RESET_ALL if COLORAMA else '')) | |
| if not found: | |
| print((Fore.RED if COLORAMA else '') + "No keys found containing the keyword." + (Style.RESET_ALL if COLORAMA else '')) | |
| else: | |
| elapsed = time() - start_time | |
| print((Fore.CYAN if COLORAMA else '') + f"Total keys found: {count} in {elapsed:.2f} seconds. Results written to {output_file}" + (Style.RESET_ALL if COLORAMA else '')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment