genpass.sh

A way to generate random text for use in passwords. [download here](http://files.technomage.net/genpass.sh.txt)
#!/bin/ksh
 
# Copyright (c) 2007, grephead.com, LLC
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of grephead.com, LLC nor the names of its
#       contributors may be used to endorse or promote products derived from
#       this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY grephead.com, LLC ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL grephead.com, LLC BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
# generate random passwords
# inspired by http://foolab.org/node/1436
# thanks to allbery [aT] ece[.]cmu[.]edu for helping with solaris9 compatibility
#
# Thanks to Jesse Trucks for pointing out that ksh is more portable.  I have
# removed the one keyword that ksh barfed on, change 'bash' to 'ksh' above if
# you want to use this with /bin/ksh instead.
# ksh93 works, ksh88 does not /bin/sh on FreeBSD does not.
# 2007-09-10 04:10 : removed |, since it could be confused with 1 or l, added -
 
PATH=/bin:/usr/bin
LC_ALL=C
LANG=C
 
# default values
count=1     # how many passwords to generate
length=10   # how long should the password be
 
print_help() {
cat << EOT
$(basename $0)
 
By default $(basename $0) will generate one, ten character password.
You can change this with the following options:
 
    To change the length of the password:
    -l <length>
 
    To change how many passwords are generated:
    -c <count>
 
EOT
}
 
# parse command line arguments
while getopts "l:c:h" Option
do 
    case "${Option}" in 
        l) length="${OPTARG}" ;;
        c) count="${OPTARG}" ;;
        h) print_help;exit ;;
    esac
done
 
# check to make sure that /dev/urandom exists
if [ -e /dev/urandom ];then
 
    for ((a=1; a <= count; a++))
    do
        ( dd if=/dev/urandom count=200 bs=1 2>/dev/null |
            tr '\n' ' ' ; echo ) |
            sed 's/[^a-kmnp-zA-HJ-NP-Z2-9\+=@#$%^&*()-]//g' |
            cut -c-"${length}"
    done
else
    echo '/dev/urandom does not exist!'
fi