Last active
January 31, 2017 17:01
-
-
Save kphrx/5da974d0af7f5042714f5246027adb42 to your computer and use it in GitHub Desktop.
This file contains 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
#!/bin/bash | |
################## | |
# # | |
# © 2015 kPherox # | |
# # | |
################## | |
CMDNAME=`basename $0` | |
CAT="cat /dev/urandom" | |
FLG_c=false | |
FLG_o=false | |
FLG_l=false | |
FLG_n=false | |
FLG_f=false | |
FLG_h=false | |
FLG_err=false | |
usage () { | |
cat <<__USAGE__ | |
Usage: | |
$CMDNAME [-c <characters>] | |
$CMDNAME [-o <characters>] | |
$CMDNAME [-c <characters>] [-l <length>] [-n <getnum>] [-f <filename>] | |
__USAGE__ | |
return 0 | |
} | |
help () { | |
cat <<__HELP__ | |
Options: | |
-c Characters. | |
-o Custom Characters. | |
-l Password length. | |
-n Get password num. | |
-f Output to file. | |
-h Display this. | |
__HELP__ | |
return 0 | |
} | |
while getopts c:o:l:n:f:h OPT | |
do | |
case $OPT in | |
"c" ) | |
FLG_c=true | |
WTYPE="$OPTARG" | |
;; | |
"o" ) | |
FLG_o=true | |
CTYPE="$OPTARG" | |
;; | |
"l" ) | |
FLG_l=true | |
WSIZE="$OPTARG" | |
;; | |
"n" ) | |
FLG_n=true | |
NLINE="$OPTARG" | |
;; | |
"f" ) | |
FLG_f=true | |
FILE="$OPTARG" | |
;; | |
"h" ) | |
FLG_h=true | |
;; | |
* ) | |
FLG_err=true | |
;; | |
esac | |
done | |
shift $(($OPTIND - 1)) | |
if $FLG_h || $FLG_err; then | |
usage | |
help | |
return 2>&- || exit 1 | |
fi | |
if ! $FLG_c && ! $FLG_o; then | |
usage | |
return 2>&- || exit 1 | |
fi | |
case ${WTYPE} in | |
"alnum" | "alpha" | "blank" | "cntrl" | "digit" | "lower" | "print" | "punct" | "space" | "upper" | "xdigit" ) | |
WTYPE="[:${WTYPE}:]" | |
;; | |
"CHAR" ) | |
WTYPE="[=CHAR=]" | |
;; | |
* ) | |
if $FLG_o; then | |
WTYPE="$CTYPE" | |
else | |
usage | |
cat <<__OPTION__ | |
"characters" list | |
alnum all letters and digits | |
alpha all letters | |
blank all horizontal whitespace | |
cntrl all control characters | |
digit all digits | |
graph all printable characters, not including space | |
lower all lower case letters | |
print all printable characters, including space | |
punct all punctuation characters | |
space all horizontal or vertical whitespace | |
upper all upper case letters | |
xdigit all hexadecimal digits | |
CHAR all characters which are equivalent to CHAR | |
__OPTION__ | |
return 2>&- || exit 1 | |
fi | |
;; | |
esac | |
LC_CTYPE=C | |
TYPE="tr -dc ${WTYPE}" | |
if ! $FLG_l; then | |
WSIZE=8 | |
fi | |
if ! $FLG_n; then | |
NLINE=3 | |
fi | |
SIZE="fold -w ${WSIZE}" | |
LINE="head -n ${NLINE}" | |
CMD=`${CAT} | ${TYPE} | ${SIZE} | ${LINE}` | |
if $FLG_f; then | |
echo "Now Creating..." | |
echo ${CMD} >> ${FILE} | |
sleep 1s | |
echo "Finish!" | |
else | |
echo "Now Creating..." | |
sleep 2s | |
echo ${CMD} | |
sleep 0.5s | |
echo "Finish!" | |
fi | |
return 2>&- || exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment