linux - Bash 脚本 : How to check if there's is only one root id?

标签 linux bash scripting audit

<分区>

如何编辑我的脚本以检查是否只有一个根 ID?

预期输出

Audit criteria: There is only one root id

Vulnerability: Yes

Details: See below


root:!:0:0::/:/usr/bin/bash

jdoe:*:0:1:John Doe:/home/jdoe:/usr/bin/bash

脚本

#!/bin/bash

isVulnerable="No"
isVulnerable="Yes"    

cat /etc/passwd | cut -f3 -d":" | sort -n | /usr/bin/uniq -c | while read x ;            
do 
   [ -z "${x}" ] && break
   set - $x

if [ "$1" -gt 1 ]; then
    users=`/bin/gawk -F: '($3 == n) { print $1 }' n=$2 /etc/passwd | /usr/bin/xargs`

echo "Audit Criteria: Duplicate UID ($2): ${users}"
echo "Vulnerability: ${isVulnerable}"
echo "Details: see below"
echo
grep "x:0:" /etc/passwd

else

echo "All user id are unique"
fi

done

最佳答案

使用AWK 收集具有重复字段的行非常方便:

get_dups() {
  awk -F':' '$3 == 0 { if (dup++) print } END { exit(dup > 1) }' /etc/passwd
}

如果 /etc/passwd 文件中有多个零用户 ID,该函数将以非零状态退出,并将具有重复根用户 ID 的行打印到标准输出。否则,退出状态为零。

用法:

dups="$(get_dups)"
if [ $? -eq 0 ]; then
  vulnerability='No'
  msg='There is only one root ID'
else
  vulnerability='Yes'
  msg='There are multiple root IDs'
fi
printf '%15s: %s\n' 'Audit criteria' "$msg"
printf '%15s: %s\n' 'Vulnerability' "$vulnerability"

[ -z "$dups" ] && dups='All user IDs are unique'
printf '\n%s\n' "$dups"

关于linux - Bash 脚本 : How to check if there's is only one root id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41798900/

相关文章:

c - Linux : receive from socket with timeout?

c - _do_fork() 如何返回两个不同的 PID(一个用于父进程,一个用于子进程)

c - 如何使用 XLib 调整像素图的大小?

linux - 将经过处理的视频从 OpenCV 传递到 FFmpeg 以进行 HLS 流式传输 (Raspberry PI)

python - 运行csh脚本时让python输入密码

regex - 如何将字符串的一部分与 bash 中的正则表达式匹配?

linux - 在 Ubuntu 中重命名 '.' 文件

linux - 为文件传输提供 ETA

windows - 更改 git bash 的根目录(/)

javascript - 从后台执行内容脚本和关联的 CSS (Manifest V3) - Chrome 扩展