我编写了这个脚本来查找$ WWWUSER具有写权限的所有文件/目录。起初,我将其余的匹配项存储在一个临时文件中。我新的必须有一种不使用文件的方式,所以这是我的“解决方案”。它可以工作,但速度很慢。有小费吗?如何优化bash脚本? (查找文件,忽略白名单,报告其余)
更新: 在包含大约7K目录和30K文件(〜8K whitelistings)脚本大约需要15分钟的目录结构...(ext3文件系统,UW320 SCSI硬盘)。
#!/usr/bin/env bash
# Checks the webroot for files owned by www daemon and
# writable at the same time. This is only needed by some files
# So we'll check with a whitelist
WWWROOT=/var/www
WWWUSER=www-data
WHITELIST=(/wp-content/uploads
/wp-content/cache
/sitemap.xml
)
OLDIFS=$IFS
IFS=$'\n'
LIST=($(find $WWWROOT -perm /u+w -user $WWWUSER -o -perm /g+w -group $WWWUSER))
IFS=$OLDIFS
arraycount=-1
whitelist_matches=0
for matchedentry in "${LIST[@]}"; do
arraycount=$(($arraycount+1))
for whitelistedentry in "${WHITELIST[@]}"; do
if [ $(echo $matchedentry | grep -c "$whitelistedentry") -gt 0 ]; then
unset LIST[$arraycount]
whitelist_matches=$(($whitelist_matches+1))
fi
done
LISTCOUNT=${#LIST[@]}
done
if [ $(echo $LISTCOUNT) -gt 0 ]; then
for item in "${LIST[@]}"; do
echo -e "$item\r"
done
echo "$LISTCOUNT items are writable by '$WWWUSER' ($whitelist_matches whitelisted)."
else
echo "No writable items found ($whitelist_matches whitelisted)."
fi
是否有可能使用任何外部应用程序来运行该程序Perl的?对于白名单引用,它可以通过使用散列来缩短并加快速度。使用不同的Shebang,它可以运行相同。 – 0xCAFEBABE 2010-11-04 10:59:13
这是可能的,但不是所期望的。但我会检查TME的想法,然后研究如何实施你的想法。谢谢! – weeheavy 2010-11-04 11:21:45