您的位置:首页 > 运维架构 > Shell

linux下检测root权限的shell脚本

2011-01-21 16:43 393 查看
个人心血来潮时写的一个脚本,比较粗糙,功能也很简单,大家有兴趣的可以拿去改改。

本脚本之争对系统中UID为0的用户

#!/bin/bash #大家都知道

echo test my computer is or not safety #显示“ test my computer is or not safety ”

root_num=`awk -F: '{print $3}' /etc/passwd|grep "^0"|wc -l` #以“:”为分隔符,打印出/etc/passwd文件中第三个域的字段,并grep以“0”开头的(意思是UID是0的用户),然后计算行数(3行代表有3个UID为0的用户,包括root),最终赋值给root_num.

if [ $root_num -eq 1 ];then #如果$root_num的值为1,表示只有一个root用户

echo your computer is safety #显示“ your computer is safety ”

else #否则

awk -F: '{print $3,$1}' /etc/passwd|grep "^0"|grep -v "root" >> hack1 #打印/etc/passwd中的第三个域(UID)和第一个域(用户名),并且grep以“0”开头的行,输出到hack1的文件中

cat hack1|awk '{print $2}'>>hack #查看hack1文件的内容,并打印出第二个域的内容,输出到hack文件中

for number in `cat hack` #查看hack的内容,并循环赋值给number

do

echo $number mybe created by hacker. #显示“ xxx mybe created by hacker.”

read -p "are you want to delete it? y or n :" answer #显示“are you want to delete it? y or n :“

if [ $answer = "y" ] #如果回答是”y“

then

userdel -r $numeber #删除xxx

echo $number is delete. #显示”xxx is dele“

if [ $answer = "yes"] #如果回答是”yes“

then

userdel -r $number

echo $number is delete.

fi

fi

done

fi

rm -f hack* #删除hack和hack1文件

有兴趣的可以试试。功能很简单,只为娱乐娱乐。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息