Search This Blog

Tuesday, February 4, 2014

Linux: Generate a random password

The main script, has to be saved in a separate file, or modified as a function:
#!/bin/bash

#add special characters you want to use in the password here:
charspool=('a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p'
'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z' '0' '1' '2' '3' '4' '5' '6' '7'
'8' '9' '0' 'A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O'
'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' 'X' 'Y' 'Z' '$' '-' '_');

len=${#charspool[*]} if [ $# -lt 1 ]; then         num=$1; else         num=$1; fi randomnumbers=$(head -c $num /dev/urandom | od -t u1 | awk '{for (i = 2; i <= NF; i++) print $i}') for c in $randomnumbers; do         echo -n ${charspool[$((c % len))]} done echo

and to call it:
lenPassword=8
# echo $(/path/to/script/gen_password.sh $lenPassword);

No comments:

Post a Comment