赞
踩
通过shell脚本统计主机用户信息,输出结果包括用户名,账户类型(Uid),属组,家目录
- #!/bin/bash
- #awk -F":" '{print $1,$3,$4,$6}' /etc/passwd >>Userinfo.txt
- function UserAndHome()
- {
- awk -F":" '{print$1"|"$6}' /etc/passwd >UserAndHome.txt
- }
- UserAndHome
- echo "Username","Type","Group","HomePath">info.csv
- for Username in `cat UserAndHome.txt|awk -F"|" '{print$1}'`
- do
- Uid=`id -u $Username`
- if [ $Uid -lt 500 ];
- then
- Type='SysAccount'
- else
- Type='PersonalAccount'
- fi
- # echo $Uid>info1.txt
- Grpname=`groups $Username|awk -F": " '{print$2}'`
- # echo $Grpname>info1.txt
- Home=`cat UserAndHome.txt|grep -w -n "^$Username"|awk -F"|" '{print$2}'`
- # echo "$Username","$Uid","$Grpname">>info1.txt
- echo "$Username","$Type","$Grpname","$Home">>info.csv
- done
- #cp info.csv /root/Desktop/info.csv
- #rm -rf info.csv
- rm -rf UserAndHome.txt
-
注意grep的精确匹配,需要用到 -w,-n"^",不然输出结果会有错误
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。