Article Listing 1 Listing 2 feb2005.tar

Listing 1 genpass

#!/bin/ksh
# Listing 1.
# genpass: This script creates a pseudo-random password.
# by randomly sampling arrays containing upper-case, lower-case,
# digits, and other characters.

LENGTH=$1
DEFAULT_LENGTH=8

upper_limit=0
for char in 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
do
   ((upper_limit = upper_limit + 1))
   upper_chars[$upper_limit]=$char
done

lower_limit=0
for char in 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
do
   ((lower_limit = lower_limit + 1))
   lower_chars[$lower_limit]=$char
done

digit_limit=0
for char in 0 1 2 3 4 5 6 7 8 9
do
   ((digit_limit = digit_limit + 1))
   digit_chars[$digit_limit]=$char
done

other_limit=0
# don't forget to escape the shell meta characters
for char in \~ ! @ \# $ % ^ \& \( \) _ + { } \| : \" \< \> ? - = [ ] \; , . /
do
   ((other_limit = other_limit + 1))
   other_chars[$other_limit]=$char
done

password=""
i=0

while (( $i < ${LENGTH:=$DEFAULT_LENGTH} ))
do
   s1=$(vmstat -s | cut -b1-9 | tr -d " \n")
   s2=$(/usr/bin/ps -elfy)
   s3=$(/usr/bin/cat -s /etc/shadow)

   RANDOM=$( echo $s1 $s2 $s3 | /usr/bin/cksum | /usr/bin/cut -b1-8)

   case $(( $RANDOM % 4 )) in
      0) next_char=${upper_chars[$(( $RANDOM % $upper_limit + 1 ))]} ;;
      1) next_char=${lower_chars[$(( $RANDOM % $lower_limit + 1 ))]} ;;
      2) next_char=${digit_chars[$(( $RANDOM % $digit_limit + 1 ))]} ;;
      3) next_char=${other_chars[$(( $RANDOM % $other_limit + 1 ))]} ;;
   esac

   password=${password}${next_char}

   (( i = i + 1 ))
done

/usr/bin/echo $password
# End Listing 1