commit 074a8c3187dfcab37114544a66aaee77f8318b9b Author: oitsjustjose Date: Tue May 13 18:31:13 2025 -0500 Initial commit with handy scripts diff --git a/README.md b/README.md new file mode 100644 index 0000000..a04eb2d --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Scripts + +These are little scripts for multiple platforms (maybe, for now just Linux). Most of the complicated ones are backed by Python, but come with installers. diff --git a/linux/python/make_random.py b/linux/python/make_random.py new file mode 100644 index 0000000..f5ed12b --- /dev/null +++ b/linux/python/make_random.py @@ -0,0 +1,20 @@ +from uuid import uuid4 +from argparse import ArgumentParser + +def generate(length: int, current = ""): + if len(current) >= length: + return current[:length] + + next = current + str(uuid4()).replace("-", "") + return generate(length, current = next) + +def main(num: int) -> None: + print(generate(num)) + +if __name__ == "__main__": + parser = ArgumentParser(prog="Make Random", description="Creates a random GUID-like value of a provided length") + parser.add_argument('num') + + args = parser.parse_args() + main(int(args.num)) + diff --git a/linux/python/scan_jars.py b/linux/python/scan_jars.py new file mode 100644 index 0000000..6002404 --- /dev/null +++ b/linux/python/scan_jars.py @@ -0,0 +1,54 @@ +import argparse +import os +import re +import sys +import zipfile + +from pathlib import Path + +def scan_jar(jar_path: Path, args) -> None: + """Opens the jar at jar_path and iterates over all files within, + determining if the file name matches + + Args: + jar_path (Path): The absolute path, resolved, to the Jar file + args (Namespace): The program's args from argparse + """ + + scan_dir = Path(args.dir).resolve() + + def __test(file_name: str) -> bool: + """Tests to see if a given filename within the jar matches the provided args + Uses regex matching if the regex flag is passed to the program + + Args: + file_name (str): The name (full path) of the file within the jar + """ + if args.regex: + try: + return re.compile(args.query).search(file_name) is not None + except re.error as err: + print(f"Regex Error! Invalid regular expression passed:\nFull Trace:\n{err}") + sys.exit(1) + else: + return args.query in file_name + + with zipfile.ZipFile(jar_path, 'r') as jar: + for file_name in jar.namelist(): + if __test(file_name): + print(f"[{jar_path.relative_to(scan_dir)}] :: {file_name}") + +def main(args) -> None: + scan_dir = Path(args.dir).resolve() + + for root, _, files in os.walk(scan_dir): + for file in list(filter(lambda x: x.endswith(".jar"), files)): + scan_jar(Path(f"{root}/{file}").resolve(), args) + +if __name__ == "__main__": + parser = argparse.ArgumentParser(prog='Jar Scanner', description='Scans jars for a given file') + parser.add_argument('query', help='The term (or regex) to search for within the filename') + parser.add_argument('-d', '--dir', default=os.getcwd(), help='The directory to scan. Defaults to cwd') + parser.add_argument('-r', '--regex', default=False, action='store_true', help='Parse and use regex any in your query.') + args = parser.parse_args() + main(args) diff --git a/linux/setup/setup_jarscan.sh b/linux/setup/setup_jarscan.sh new file mode 100755 index 0000000..115fb12 --- /dev/null +++ b/linux/setup/setup_jarscan.sh @@ -0,0 +1,26 @@ +#!/bin/bash +cmd_name="jarscan" +script_fn="scan_jars.py" + +# Re-run the script as root if we're not +if [ "$EUID" != 0 ]; then + sudo "$0" "$@" + exit $? +fi + +echo "Setting up $cmd_name" +if ! command -v /usr/bin/python3 2>&1 >/dev/null +then + echo "/usr/bin/python3 does not exist! Please install it before continuing..." + exit 1 +fi + +mkdir -p /opt/scripts +cp "../python/$script_fn" "/opt/scripts/$script_fn" + +# Create executable as a script, install to local/bin +echo "/usr/bin/python3 /opt/scripts/$script_fn" > $cmd_name +chmod +x $cmd_name +mv $cmd_name /usr/local/bin + +echo "Done! Try the command '$cmd_name' to see the effects!" \ No newline at end of file diff --git a/linux/setup/setup_makerand.sh b/linux/setup/setup_makerand.sh new file mode 100755 index 0000000..6db451a --- /dev/null +++ b/linux/setup/setup_makerand.sh @@ -0,0 +1,26 @@ +#!/bin/bash +cmd_name="makerand" +script_fn="make_random.py" + +# Re-run the script as root if we're not +if [ "$EUID" != 0 ]; then + sudo "$0" "$@" + exit $? +fi + +echo "Setting up $cmd_name" +if ! command -v /usr/bin/python3 2>&1 >/dev/null +then + echo "/usr/bin/python3 does not exist! Please install it before continuing..." + exit 1 +fi + +mkdir -p /opt/scripts +cp "../python/$script_fn" "/opt/scripts/$script_fn" + +# Create executable as a script, install to local/bin +echo "/usr/bin/python3 /opt/scripts/$script_fn" > $cmd_name +chmod +x $cmd_name +mv $cmd_name /usr/local/bin + +echo "Done! Try the command '$cmd_name' to see the effects!" \ No newline at end of file