存档

文章标签 ‘md5’

生成md5密码

2007年1月3日 16hot 没有评论

是使用md5命令,生成md5码,然后截取指定位数作为密码。用于用户密码,不如系统用户,mysql用户等。

#! /bin/sh
# build md5 password
# http://16hot.blog.isyi.com
# 16hot
# 2006-12-31
# $Id: genpw.sh 435 2006-12-31 03:47:55Z 16hot $

ROUND=”YES” ;

SCRIPTNAME=”genpw.sh” ;
MD5=”/sbin/md5″ ;
AWK=”/usr/bin/awk” ;
CUT=”/usr/bin/cut” ;

if [ "$1" = "" ] ; then
echo ;
echo “Syntax: $SCRIPTNAME <key> [length]” ;
echo “Example: ./$SCRIPTNAME my’spassword” ;
echo “Author: 16hot ” ;
echo “Home: http://16hot.blog.isyi.com”;
echo ;
exit ;
else
KEY=”$1″ ;
fi ;

if [ "$2" = "" ] ; then
LEN=”16″ ;
else

test $2 -gt 5 -a $2 -lt 33 2>/dev/null
if [ "$?" != "0" ] ; then
echo ;
echo “The password length must between 6 and 32″ ;
echo “set to default length: 16″ ;
#echo ;
LEN=”16″;
else
LEN=”$2″ ;
fi ;
fi ;

if [ ! -x ${MD5} ] ; then
echo ;
echo “md5 command not found!” ;
echo “Please setup MD5!” ;
echo ;
exit ;
fi ;

if [ ! -x ${AWK} ] ; then
echo ;
echo “awk command not found!” ;
echo “Please install awk and setup AWK” ;
echo ;
exit ;
fi ;

if [ ! -x ${CUT} ] ; then
echo ;
echo “cut command not found!” ;
echo “Please install cut and setup CUT” ;
echo ;
exit ;
fi ;

if [ "${ROUND}" = "YES" ] ; then
NewKey=”${KEY}-genpw-`date +%Y%m%d%H%M%S`” ;
else
NewKey=”${KEY}-genpw” ;
fi ;

#echo ${KEY};
#echo ${NewKey};

## Get OS’s name
OS=”`uname -s`”;

## becuse the diffent OS’s md5 program have diffrent
##
if [ "${OS}" = "FreeBSD" ] ; then
PW=`${MD5} -s “${NewKey}” |  awk -F= ‘{print $2}’ | awk ‘{print $1}’ | ${CUT} -b1-${LEN}` ;
#PW=`${MD5} -s “${NewKey}”` ;
else
echo “Sorry !! only support FreeBSD now.” ;
exit ;
fi ;

echo ;
echo “PW-${LEN}: { ${PW} }” ;
echo ;
echo “Author: 16hot ” ;
echo “Home: http://16hot.blog.isyi.com”;
echo ;
## end script

分类: Shell 标签: ,