前段时间接到一个任务,需要添加一些人访问web的权限.该站点只是用web形式给用户下载文件,通过htaccess控制访问.所以就有了如下代码:
#!/bin/sh
# add_webuser.sh passwd_path directory
#
error()
{
echo "$@" 1>&@
usage_and_exit 1
}
usage()
{
echo "Usage: $PROGRAM Password_path authorization_path"
}
usage_and_exit()
{
usage
exit $1
}
PROGRAM=add_webuser
PASSWD_PATH=$1
DIRECTORY_PATH=$2
if [ $PASSWD_PATH = ""]
then
error
fi
var0=0
var1=`wc -l userfile | awk '{print $1}'`
cp $DIRECTORY_PATH/.htaccess /data/backup/htaccess.`date '+%Y-%m-%d-%H-%M-%S'`.bak
cat $DIRECTORY_PATH/.htaccess|grep require > /tmp/authorfile
PASSWD_FILE=`cat $DIRECTORY_PATH/.htaccess |grep AuthUserFile |awk '{print $2}'`
echo $PASSWD_FILE
cp $PASSWD_FILE /data/backup/password($PASSWD_FILE).`date '+%Y-%m-%d-%H-%M-%S'`.bak
while [ "$var0" -lt $var1 ]
do
var0=$[var0+1]
LINE=`sed -n $var0'p' /home/kennys/user_passwd`
WEBUSER=`echo $LINE | awk '{print $1}'`
WEBPASSWD=`echo $LINE | awk '{print $2}'`
echo `cat /tmp/authorfile` $WEBUSER > /tmp/authorfile
htpasswd -b $PASSWD_PATH $WEBUSER $WEBPASSWD
done
echo 'AuthType Basic' > /tmp/.htaccess
echo 'AuthName "teleweb"' >> /tmp/.htaccess
echo 'AuthUserFile $PASSWD_PATH' >> /tmp/.htaccess
echo '' >> /tmp/.htaccess
cat /tmp/authorfile >> /tmp/.htaccess
echo '' >> /tmp/.htaccess
mv /tmp/.htaccess $DIRECTORY_PAHT/.htaccess
rm -f /tmp/authorfile
阅读(1051) | 评论(0) | 转发(0) |