Refactor
This commit is contained in:
parent
30bd30e038
commit
71c8ea7bbd
41 changed files with 375 additions and 284 deletions
1
Shell Scripts/python/.gitignore
vendored
Normal file
1
Shell Scripts/python/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
__pycache__
|
||||
19
Shell Scripts/python/convert_heic.py
Normal file
19
Shell Scripts/python/convert_heic.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
from PIL import Image
|
||||
from pillow_heif import register_heif_opener
|
||||
from pathlib import Path
|
||||
from sys import argv
|
||||
|
||||
def main() -> None:
|
||||
if len(argv) < 2:
|
||||
return
|
||||
|
||||
path = Path(argv[1]).resolve()
|
||||
if path.suffix.lower() != ".heic":
|
||||
return
|
||||
|
||||
register_heif_opener()
|
||||
im = Image.open(path)
|
||||
im.save(path.with_suffix(".jpeg"), quality=100)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
70
Shell Scripts/python/make_random.py
Normal file
70
Shell Scripts/python/make_random.py
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
from argparse import ArgumentParser
|
||||
from random import randint
|
||||
from uuid import uuid4
|
||||
from utils import copy_to_clipboard
|
||||
|
||||
def generate_uuid(length: int, current="") -> str:
|
||||
if len(current) >= length:
|
||||
return current[:length]
|
||||
|
||||
next = current + str(uuid4()).replace("-", "")
|
||||
return generate_uuid(length, current=next)
|
||||
|
||||
|
||||
def generate(length: int, chars: str, current="") -> str:
|
||||
if len(current) >= length:
|
||||
return current[:length]
|
||||
|
||||
next = current + chars[randint(0, (len(chars) - 1))]
|
||||
|
||||
return generate(length, chars, current=next)
|
||||
|
||||
|
||||
def main(args) -> None:
|
||||
num = int(args.num)
|
||||
if args.uuid_only:
|
||||
# Max recursion depth workaround
|
||||
rem, accumulator = num, ""
|
||||
while rem > 0:
|
||||
amt = min(128, rem)
|
||||
accumulator += generate_uuid(amt)
|
||||
rem -= amt
|
||||
|
||||
if args.copy_to_clipboard:
|
||||
copy_to_clipboard(accumulator)
|
||||
print("Copied to clipboard!")
|
||||
else:
|
||||
print(accumulator)
|
||||
else:
|
||||
exclusions = list(map(lambda x: str(x).strip(), args.exclude_chars.split(",")))
|
||||
char_choices = "ABCDEFGHIJKLMONPQRSTUVWXYZabcdefghijklmonpqrstuvwxyz1234567890!@#$%^&*()-=_+[]{}\\|;:,<.>/?`~"
|
||||
|
||||
for exclusion in exclusions:
|
||||
char_choices = char_choices.replace(exclusion, "")
|
||||
|
||||
# Max recursion depth workaround
|
||||
rem, accumulator = num, ""
|
||||
while rem > 0:
|
||||
amt = min(128, rem)
|
||||
accumulator += generate(amt, char_choices)
|
||||
rem -= amt
|
||||
|
||||
if args.copy_to_clipboard:
|
||||
copy_to_clipboard(accumulator)
|
||||
print("Copied to clipboard!")
|
||||
else:
|
||||
print(accumulator)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = ArgumentParser(
|
||||
prog="Make Random",
|
||||
description="Creates a random GUID-like value of a provided length",
|
||||
)
|
||||
parser.add_argument("num")
|
||||
parser.add_argument("-u", "--uuid-only", action="store_true")
|
||||
parser.add_argument("-e", "--exclude-chars", default="")
|
||||
parser.add_argument("-c", "--copy-to-clipboard", action="store_true")
|
||||
|
||||
args = parser.parse_args()
|
||||
main(args)
|
||||
19
Shell Scripts/python/new-uuid.py
Normal file
19
Shell Scripts/python/new-uuid.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
from argparse import ArgumentParser
|
||||
from uuid import uuid4
|
||||
|
||||
from utils import copy_to_clipboard
|
||||
|
||||
|
||||
def main(args):
|
||||
if args.copy_to_clipboard:
|
||||
copy_to_clipboard(str(uuid4()))
|
||||
print("Copied to clipboard!")
|
||||
else:
|
||||
print(str(uuid4()))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = ArgumentParser(prog="new-uuid")
|
||||
parser.add_argument("-c", "--copy-to-clipboard", action="store_true")
|
||||
args = parser.parse_args()
|
||||
main(args)
|
||||
2
Shell Scripts/python/requirements.txt
Normal file
2
Shell Scripts/python/requirements.txt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pillow==11.3.0
|
||||
pillow_heif==1.1.1
|
||||
85
Shell Scripts/python/scan_jars.py
Normal file
85
Shell Scripts/python/scan_jars.py
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
import os
|
||||
import re
|
||||
from argparse import ArgumentParser
|
||||
from pathlib import Path
|
||||
from sys import exit
|
||||
from zipfile import ZipFile
|
||||
|
||||
|
||||
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(scan_target: str) -> bool:
|
||||
"""Scans the scan_target to see if it contains the queries from the parsed args.
|
||||
If the args instruct us to use regex, compile the regex and attempt to use it
|
||||
|
||||
Args:
|
||||
scan_target (str): The text to scan for - can be a filename or file contents
|
||||
|
||||
Returns:
|
||||
bool: True if the scan_target contains the query or regex
|
||||
"""
|
||||
if args.regex:
|
||||
try:
|
||||
return re.compile(args.query).search(scan_target) is not None
|
||||
except re.error as err:
|
||||
print(f"Regex Error! Invalid regular expression passed:\nFull Trace:\n{err}")
|
||||
exit(1)
|
||||
else:
|
||||
return args.query in scan_target
|
||||
|
||||
with ZipFile(jar_path, "r") as jar:
|
||||
for file_name in jar.namelist():
|
||||
if args.deep: # Perform a deep scan of the jar itself
|
||||
with jar.open(file_name) as file:
|
||||
try:
|
||||
if __test(file.read().decode()):
|
||||
print(f"[{jar_path.relative_to(scan_dir)}] :: {file_name}")
|
||||
except UnicodeDecodeError:
|
||||
pass # This just means the file was a binary / non-text file
|
||||
|
||||
elif __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 = 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.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-D",
|
||||
"--deep",
|
||||
default=False,
|
||||
action="store_true",
|
||||
help="Rather than searching the file *name* for the given term (or regex), searches the contents of files instead",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
main(args)
|
||||
54
Shell Scripts/python/utils.py
Normal file
54
Shell Scripts/python/utils.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import os
|
||||
from platform import release, system
|
||||
from subprocess import check_call
|
||||
from sys import executable as python
|
||||
from tempfile import mktemp
|
||||
|
||||
from requests import get as fetch
|
||||
|
||||
|
||||
def copy_to_clipboard(val: str) -> bool:
|
||||
sys = system()
|
||||
copy_cmd = ""
|
||||
if sys == "Windows":
|
||||
copy_cmd = "clip.exe"
|
||||
elif sys == "macOS":
|
||||
copy_cmd = "pbcopy"
|
||||
elif sys == "Linux":
|
||||
if "WSL" in release():
|
||||
copy_cmd = "clip.exe" # Windows native clip.exe still works in WSL
|
||||
else:
|
||||
copy_cmd = "xclip -selection clipboard"
|
||||
else:
|
||||
print(f"System {sys} is not supported - cannot copy to clipboard")
|
||||
return False
|
||||
|
||||
tmp_path = mktemp()
|
||||
with open(tmp_path, "w", encoding="utf-8") as fh:
|
||||
fh.write(val.strip())
|
||||
|
||||
try:
|
||||
ret_val = check_call(f"cat {str(tmp_path)} | {copy_cmd}", shell=True)
|
||||
finally:
|
||||
os.unlink(tmp_path)
|
||||
|
||||
return ret_val == 0
|
||||
|
||||
|
||||
def install_package(package_name: str) -> None:
|
||||
def _prereq() -> None:
|
||||
res = os.system(f"{python} -m pip")
|
||||
if res == 0:
|
||||
return
|
||||
|
||||
data = fetch("https://bootstrap.pypa.io/get-pip.py")
|
||||
filename = mktemp()
|
||||
with open(filename, "w", encoding="utf-8") as fh:
|
||||
fh.write(data.text)
|
||||
|
||||
os.system(f"{python} {filename}")
|
||||
os.unlink(filename)
|
||||
|
||||
_prereq()
|
||||
os.system(f"{python} -m pip install {package_name}")
|
||||
print(f"Package {package_name} has been installed - please rerun this command!")
|
||||
24
Shell Scripts/python/wifi_qr.py
Normal file
24
Shell Scripts/python/wifi_qr.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
from argparse import ArgumentParser
|
||||
|
||||
from utils import install_package
|
||||
|
||||
try:
|
||||
import qrcode
|
||||
except ImportError:
|
||||
install_package("qrcode[pil]")
|
||||
finally:
|
||||
import qrcode
|
||||
|
||||
|
||||
def main(args):
|
||||
img = qrcode.make(f"WIFI:S:{args.ssid};T:WPA;P:{args.passwd};;")
|
||||
img.save(args.output)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = ArgumentParser(prog="WiFi QR Generator", description="Creates QR Codes to join a Password Protected WiFi Network")
|
||||
parser.add_argument("ssid", help="The SSID of the WiFi Network to connect to")
|
||||
parser.add_argument("passwd", help="The passphrase of the WiFi Network to connect to")
|
||||
parser.add_argument("-o", "--output", required=True, help="Where to save the resulting image to")
|
||||
args = parser.parse_args()
|
||||
main(args)
|
||||
27
Shell Scripts/setup/setup_jarscan.sh
Normal file
27
Shell Scripts/setup/setup_jarscan.sh
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#!/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"
|
||||
cp "../python/utils.py" "/opt/scripts/utils.py"
|
||||
|
||||
# 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!"
|
||||
27
Shell Scripts/setup/setup_makerand.sh
Normal file
27
Shell Scripts/setup/setup_makerand.sh
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#!/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"
|
||||
cp "../python/utils.py" "/opt/scripts/utils.py"
|
||||
|
||||
# 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!"
|
||||
27
Shell Scripts/setup/setup_uuid.sh
Normal file
27
Shell Scripts/setup/setup_uuid.sh
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#!/bin/bash
|
||||
cmd_name="new-uuid"
|
||||
script_fn="new-uuid.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"
|
||||
cp "../python/utils.py" "/opt/scripts/utils.py"
|
||||
|
||||
# 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!"
|
||||
27
Shell Scripts/setup/setup_wifiqr.sh
Normal file
27
Shell Scripts/setup/setup_wifiqr.sh
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#!/bin/bash
|
||||
cmd_name="wifi-qr"
|
||||
script_fn="wifi_qr.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"
|
||||
cp "../python/utils.py" "/opt/scripts/utils.py"
|
||||
|
||||
# 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!"
|
||||
Loading…
Reference in a new issue