잠김, 잠금 해제, 만료된 사용자 계정 목록 수를 가져오는 명령

잠김, 잠금 해제, 만료된 사용자 계정 목록 수를 가져오는 명령

사용자 수, 잠긴 사용자 계정 수, 잠금 해제된 사용자 계정 수, 만료되지 않도록 설정된 비밀번호 수를 가져오는 세 가지 명령이 있습니다.

    cut -d: -f1 /etc/passwd|wc -l
    cat /etc/passwd | cut -d : -f 1 | awk '{ system("passwd -S " $0) }'|wc -l
 awk -F: '{ system("passwd -S " $1)}' /etc/passwd | grep " PS "|wc -l
    cut -f 1 -d: /etc/passwd | xargs -n 1 -I {} bash -c ' echo -n {}" "; chage -l {} | fgrep "Password expires"' | column -t|wc -l

echo 명령을 추가하거나 하나의 명령을 사용하여 이 모든 정보를 얻고 만료되도록 설정된 계정 정보를 얻을 수 있는 방법이 있습니까? echo "사용자 수=xx", 잠긴 사용자 수=xx, 비밀번호가 만료되지 않는 사용자 수=xx, 만료된 사용자 수를 설정하시겠습니까?

감사해요

답변1

awk세 가지 중 두 가지(사용자 수 및 잠긴 계정 수)를 ed 와 쉽게 결합할 수 있지만 만료된 비밀번호를 표시하려면 passwd -Sa이들을 반복해야 합니다 .chage

passwd -Sa |  awk 'BEGIN {lockedusers=0} $2 ~ /L/ {lockedusers++} END {print "Total users:",NF;print "Locked users:",lockedusers}'
neuser=0
for user in $(cut -f1 -d: /etc/passwd); do 
   if chage -l "$user" | grep -i '^Password expires' | grep -q never; then 
      neuser=$((neuser+1))
   fi
done
echo "Non-expiring users: $neuser"

구현이 passwd부족한 경우 -a(예: Red Hat 파생 배포판) 유사한 루프를 사용할 수 있습니다 for.

neuser=0
for user in $(cut -f1 -d: /etc/passwd); do
   if chage -l "$user" | grep -i '^Password expires' | grep -q never; then
      neuser=$((neuser+1))
   fi
   passwd -S "$user"
done | awk 'BEGIN {lockedusers=0} $2 ~ /L/ {lockedusers++} END {print "Total users:",NF;print "Locked users:",lockedusers}'
echo "Non-expiring users: $neuser"

답변2

다음 스크립트는 만료되었으며 향후 7일 이내에 만료될 사용자 목록을 반환합니다.

cat /etc/shadow | cut -d: -f1,8 | sed /:$/d > /tmp/expirelist.txt
totalaccounts=`cat /tmp/expirelist.txt | wc -l`
for((i=1; i<=$totalaccounts; i++ ))
do
    tuserval=`head -n $i /tmp/expirelist.txt | tail -n 1`
    username=`echo $tuserval | cut -f1 -d:`
    userexp=`echo $tuserval | cut -f2 -d:`
    userexpireinseconds=$(( $userexp * 86400 ))
    todaystime=`date +%s`
    #check if the user expired or not?
    if [ $userexpireinseconds -ge $todaystime ] ;
    then
        timeto7days=$(( $todaystime + 604800 ))
    if [ $userexpireinseconds -le $timeto7days ];
    then
        echo "The user account $username going to expired in next 7 days"
    fi
    else
        echo  "The user account $username already expired"
    fi
done

관련 정보