103 lines
2.8 KiB
Shell
Executable file
103 lines
2.8 KiB
Shell
Executable file
#!/bin/bash
|
|
|
|
# Source - https://stackoverflow.com/a/7069755
|
|
# Posted by Flexo - Save the data dump, modified by community. See post 'Timeline' for change history
|
|
# Retrieved 2026-06-20, License - CC BY-SA 4.0
|
|
|
|
while test $# -gt 0; do
|
|
case "$1" in
|
|
-h|--help)
|
|
echo "makerand - generate a random string"
|
|
echo " "
|
|
echo "Usage:"
|
|
echo " makerand char-count [-e,--exclude-chars=a,b,c,d,e] [-u,--uuid-only]"
|
|
echo " "
|
|
echo "options:"
|
|
echo "-h, --help Show this prompt"
|
|
echo "-e,--exclude-chars A comma-delimited list of characters to exclude"
|
|
echo "-u,--uid-only Only use [0-9,A-F] characters"
|
|
exit 0
|
|
;;
|
|
-e)
|
|
shift
|
|
if test $# -gt 0; then
|
|
export EXCLUDED=$1
|
|
else
|
|
echo "No excluded characters were specified!"
|
|
exit 1
|
|
fi
|
|
shift
|
|
;;
|
|
--exclude-chars*)
|
|
export EXCLUDED=`echo $1 | sed -e 's/^[^=]*=//g'`
|
|
shift
|
|
;;
|
|
-u|--uuid-only)
|
|
export ONLY_USE_UUID=1
|
|
shift
|
|
;;
|
|
*)
|
|
if test -z "$COUNT"; then
|
|
export COUNT=$1
|
|
shift
|
|
else
|
|
echo "Only one count can be provided!"
|
|
exit 1
|
|
fi
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if test -z "$COUNT"; then
|
|
echo "Character count is required!"
|
|
exit 1
|
|
fi
|
|
|
|
|
|
# Set up the python environment
|
|
if [ ! -f "/tmp/shell-scripts/venv/bin/python" ]; then
|
|
mkdir -p /tmp/shell-scripts
|
|
python3 -m venv /tmp/shell-scripts/venv > /dev/null 2>&1
|
|
fi
|
|
|
|
# Finally run the script
|
|
/tmp/shell-scripts/venv/bin/python << EOF
|
|
from argparse import ArgumentParser
|
|
from random import randint
|
|
from uuid import uuid4
|
|
|
|
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)
|
|
|
|
num = int("$COUNT")
|
|
chars = "0123456789ABCDEF" if "$ONLY_USE_UUID" == "1" else "ABCDEFGHIJKLMONPQRSTUVWXYZabcdefghijklmonpqrstuvwxyz1234567890!@#$%^&*()-_=+[{]}|;:,<.>/?\`~"
|
|
# Support for splitting by commas or not
|
|
exclusions = list("".join(map(lambda x: x.strip(), "$EXCLUDED".split(","))))
|
|
|
|
for exclusion in exclusions:
|
|
print(exclusion)
|
|
chars = chars.replace(exclusion, "")
|
|
|
|
# Max recursion depth workaround
|
|
rem, accumulator = num, ""
|
|
|
|
while rem > 0:
|
|
amt = min(128, rem)
|
|
accumulator += generate(amt, chars)
|
|
rem -= amt
|
|
|
|
print(accumulator)
|
|
|
|
EOF
|
|
|