shell小技巧(四十三)用户登录管理(禁止或恢复登录)

脚本接受参数,恢复或者禁止指定用户登录

代码:

#!/bin/bash
if [ -z $1 ]; then
   echo "please input e or d.e is enable,d is disable!"
   exit 1
elif [ $1 == "e" ] || [ $1 == "d" ]; then
   act=$1
else
   echo "input error!please input e or d.e is enable,d is disable!"
   echo $1
   exit 1
fi

if [ -z $2 ]; then
   echo "please input one username"
   exit 1
else
   username=$2
fi
j=$(grep ${username}":" /etc/passwd|wc -l)
u=$(egrep "^${username}:" /etc/passwd|cut -d ":" -f3)
#echo $u
#exit 0
if [ $j -eq 0 ]; then
   echo "user not exist!"
   exit 1
else 
   if [ $u -eq 0 ]; then
      echo "please input another username!"
      exit 1
   else
      if [ $act == "d" ]; then
         echo $username " can not login"
         usermod -s /usr/sbin/nologin $username
      elif [ $act == "e" ]; then
         echo $username "can login"
         usermod -s /bin/bash $username
      fi
   fi
fi
 

发布了95 篇原创文章 · 获赞 3 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/bigwood99/article/details/105384660