环境现状:客户需要将给定的一些目录进行owner、group和文件权限模式进行检测,给定的文件放在window下的execl格式中。目录的层次以一个空格的形式隔开即:/test/test1的话表示为:
/test
(一个空格)/test1
依次类推。
首先需要创建模拟环境。
1.在redhat上创建基本的测试环境如下:
#!/bin/bash
directory1=test1
directory2=test2
directory3=test3
mkdir -p /${directory1}/{${directory2},${directory3}}/hello/world
mkdir -p /${directory2}/${directory3}/test
for username in a1 a2
do
useradd $username > /dev/null 2>&1
resultCode=${?}
if [ ${resultCode} -eq 0 ];then
echo "user [$username] has been created."
else
echo "user [$username] has not been created.failed!"
fi
done
chmod 700 /${directory1}/${directory2}/hello
chown a1.root /${directory1}/${directory2}/hello
chmod 711 /${directory1}/${directory2}/hello/world
chown a1.a2 /${directory1}/${directory2}/hello/world
chmod 777 /${directory2}/${directory3}/test
chown a2.a1 /${directory2}/${directory3}/test
echo " end "
[root@stationx test]# sh 11.sh
user [a1] has been created.
user [a2] has been created.
end
2.将execl保存的文件格式转为linux中的匹配格式
/test1 root root drwxr-xr-x
/test2 root root drwxr-xr-x
/hello a1 root drwx------
/world a1 a2 drwx--x--x
/test3 root root drwxr-xr-x
/hello root root drwxr-xr-x
/world root root drwxr-xr-x
/test2 root root drwxr-xr-x
/test3 root root drwxr-xr-x
/test a2 a1 drwxrwxrwx
[root@stationx test]# ./exchange.awk 11.txt
[root@stationx test]# ls
11.sh 11.txt a2 a4 check.sh exchange.awk
[root@stationx test]# cat a4
/test1 root root drwxr-xr-x
-/test2 root root drwxr-xr-x
--/hello a1 root drwx------
---/world a1 a2 drwx--x--x
-/test3 root root drwxr-xr-x
--/hello root root drwxr-xr-x
---/world root root drwxr-xr-x
/test2 root root drwxr-xr-x
-/test3 root root drwxr-xr-x
--/test a2 a1 drwxrwxrwx
3.进行检测
[root@stationx test]# ./check.sh a4
user check. OK! group check. OK! mode check. OK!
user check. OK! group check. OK! mode check. OK!
user check. OK! group check. OK! mode check. OK!
user check. OK! group check. OK! mode check. OK!
user check. OK! group check. OK! mode check. OK!
user check. OK! group check. OK! mode check. OK!
user check. OK! group check. OK! mode check. OK!
user check. OK! group check. OK! mode check. OK!
user check. OK! group check. OK! mode check. OK!
user check. OK! group check. OK! mode check. OK!
4.设置不匹配的项目进行检测
1)首先设置用户不存在的项进行检测:
[root@stationx test]# cat a4
/test1 root456 root drwxr-xr-x
-/test2 root root drwxr-xr-x
--/hello a1 root drwx------
---/world a1 a2 drwx--x--x
-/test3 root root drwxr-xr-x
--/hello root root drwxr-xr-x
---/world root root drwxr-xr-x
/test2 root root drwxr-xr-x
-/test3 root root drwxr-xr-x
--/test a2 a1 drwxrwxrwx
[root@stationx test]# ./check.sh a4
The user--->root456 is not system user,Please create it first!
2)组不存在的项进行检测:
[root@stationx test]# cat a4
/test1 root root7 drwxr-xr-x
-/test2 root root drwxr-xr-x
--/hello a1 root drwx------
---/world a1 a2 drwx--x--x
-/test3 root root drwxr-xr-x
--/hello root root drwxr-xr-x
---/world root root drwxr-xr-x
/test2 root root drwxr-xr-x
-/test3 root root drwxr-xr-x
--/test a2 a1 drwxrwxrwx
[root@stationx test]# ./check.sh a4
The group--->root7 is not system group,Please create it first!
3)存在用户但是不是想要的用户检测
[root@stationx test]# cat a4
/test1 root root drwxr-xr-x
-/test2 apache root drwxr-xr-x
--/hello a1 root drwx------
---/world a1 a2 drwx--x--x
-/test3 root root drwxr-xr-x
--/hello root root drwxr-xr-x
---/world root root drwxr-xr-x
/test2 root root drwxr-xr-x
-/test3 root root drwxr-xr-x
--/test a2 a1 drwxrwxrwx
[root@stationx test]# ./check.sh a4
user check. OK! group check. OK! mode check. OK!
user check failed: the directory--->[/test1/test2] owner is ---> [root]
group check. OK! mode check. OK!
user check. OK! group check. OK! mode check. OK!
user check. OK! group check. OK! mode check. OK!
user check. OK! group check. OK! mode check. OK!
user check. OK! group check. OK! mode check. OK!
user check. OK! group check. OK! mode check. OK!
user check. OK! group check. OK! mode check. OK!
user check. OK! group check. OK! mode check. OK!
user check. OK! group check. OK! mode check. OK!
同样组和权限的测试一样去试着检测。
由于只能上传三个附件,对于转换格式的awk脚本我就拷贝在文章里。
[root@stationx test]# cat exchange.awk
#!/bin/awk -f
BEGIN{ FS="/"
OFS="/"
}
{file=ARGV[1]}
{
while(getline < file > 0)
{
#print $0
if($1~/ /){
gsub(" ","-",$1)
print $0 > "a4"
}
else{
print $0 > "a4"
}
}
}
在上传文件的时候我将.sh格式的文件改为了.txt格式,下载后再改回.sh格式执行即可。
转载于:https://blog.51cto.com/perfect/885308