1、安装httpd,并启动
yum install -y httpd
yum --now enable httpd
2、配置basic认证,修改如下部分
<Directory "/var/www/cgi-bin">
AuthType Basic
AuthName "Require Login"
#认证文件
AuthUserFile "/etc/httpd/password"
Require valid-user
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
#创建认证文件,test是用户名,回车后输入密码
htpasswd -c /etc/httpd/password test
3、将脚本文件放在/var/www/cgi-bin/目录下,并添加执行权限
cd /var/www/cgi-bin
chmod +x logsearch
这里是一个根据关键字在日志文件里查询的脚本,$QUERY_STRING为浏览器中提交的内容
apache是以普通用户apache启动的,要注意查询的日志文件,apache用户要有访问权限
#!/bin/bash
function getdir(){
for file in `ls $1`
do
if [[ $file =~ "data" ]];then
if [[ $file =~ "error" || $file =~ "gz" ]];then
echo '' >/dev/null
else
if [[ $LOGPATH =~ "$file" ]];then
echo '<option value="'$file'" selected="selected" >'$file'</option>'
else
echo '<option value="'$file'" >'$file'</option>'
fi
fi
fi
done
}
LOGBASEPATH=xxx
NUM=`echo "$QUERY_STRING" | sed -n 's/^.*num=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
LOGPATH=`echo "$QUERY_STRING" | sed -n 's/^.*logpath=\([^&]*\).*$/\1/p' | sed "s/%20/ /g" | sed "s/%2F/\//g"`
echo "Content-type: text/html"
echo ""
echo '<html>'
echo '<head>'
echo '<title>PileLog Search 02</title>'
echo '</head>'
echo '<body>'
echo "<form method=GET action=\"${script}\">"\
echo '<br>PILE__NUM: <input type="text" name="num" size=50 value="'$NUM'">'
echo '<br>LOG_PATH: <select name="logpath">'
getdir $LOGBASEPATH
echo '</select>'
echo '<br><input type="submit" value="Search">'
if [ -z "$QUERY_STRING" ];then
exit 0
else
cd $LOGBASEPATH
if [ -f "$LOGPATH" ];then
sed -n '/'"$NUM"'/p' $LOGPATH > /tmp/res.log
echo "<br>"
cat /tmp/res.log | while read line
do
echo $line
echo "<br>"
done
echo "<br>"
else
echo "<br>"
echo "$LOGPATH not exits"
echo "$VIN"
fi
fi
echo '</body>'
echo '</html>'
exit 0
4、浏览器查看效果