Here's some potential useful feature from my .profile. It might not be
perfect, but at least it's better then some of the generators I've seen
in production.
# $RANDOM is not portable and in ksh it's limited to 32767.
rand() {
local number
local rdevice
for rdevice in /dev/{u,}random fail; do
test -c $rdevice -a -r $rdevice && break
done
test $rdevice = "fail" && return 1
number=$(dd if=$rdevice bs=4 count=1 2>/dev/null | hexdump -e '/4
"%u\n"')
if [ -z "$1" ]; then
echo $number
else
echo $(($number % $(($1 + 1))))
fi
}
# http://xkcd.com/936/
generatepw_file() {
local file=${1:-/usr/share/dict/words}
local lineno=`wc -l < $file` || return 1
local i=0
local passphrase
local random
if [ $lineno -lt 75000 ]; then
echo "Not enough words in $file" >&2
return 1
fi
while [ $i -lt 4 ]; do
random=$(rand $lineno) || return 1
passphrase="${passphrase}$(sed -n "$random p" $file)"
i=$((i+1));
done
echo $passphrase
}
generatepw_random() {
local
characters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+[]{}:,.<>/?\|'
local charactercount=$(echo -n $characters | wc -c)
local password=''
# Password should be somewhere between 10 and 20 characters
local passwordlength=$((`rand 10` + 10))
local i=0
local random
while [ $i -lt $passwordlength ]; do
random=$(rand ${charactercount}) || return 1
password=${password}$(echo $characters | sed -n
"s/^.\{${random}\}\(.\).*/\1/p")
i=$((i+1))
done
echo $password
}
generatepw() {
if [ `rand 1` = 1 ]; then
generatepw_file || generatepw_random || return 1
else
generatepw_random || return 1
fi
}
On 08/28/15 02:36, T B wrote:
Resurrecting this not-too-old thread. You might find this one useful if
you run CARP firewalls which gives you a dynamic prompt telling you the
master/backup/other status.
function fwStatus {
IFCONFIG=`ifconfig -a | grep carp:`
NUMCARPS=`echo "$IFCONFIG" | wc -l`
BACKUPCARPS=`echo "$IFCONFIG" | grep 'carp: BACKUP' | wc -l`
MASTERCARPS=`echo "$IFCONFIG" | grep 'carp: MASTER' | wc -l`
if [[ "$MASTERCARPS" == "$NUMCARPS" ]]; then
printf master
elif [[ "$BACKUPCARPS" == "$NUMCARPS" ]]; then
printf backup
else
printf other
fi
}
HOSTNAME=`hostname -s`
PS1='${USER}@${HOSTNAME}:${PWD} ($(fwStatus)) $ '