Skip to content

Instantly share code, notes, and snippets.

@tuxnker
Created March 7, 2016 12:20
Show Gist options
  • Save tuxnker/4c77d3b64a179bc0481f to your computer and use it in GitHub Desktop.
Save tuxnker/4c77d3b64a179bc0481f to your computer and use it in GitHub Desktop.
#!/bin/bash
fn_lsuser(){
USER=$1
DATA1=`grep -w $USER /etc/passwd`
SNAME=`echo $DATA1 | cut -d: -f1`
SID=`echo $DATA1 | cut -d: -f3`
SGPNUM=`echo $DATA1 | cut -d: -f4`
SHOME=`echo $DATA1 | cut -d: -f6`
SSHELL=`echo $DATA1 | cut -d: -f7`
SGCOS=`echo $DATA1 | cut -d: -f5`
DATA2=`grep -w $SGPNUM /etc/group`
SGROUP=`echo $DATA2 | cut -d: -f1`
DATA3=`grep -w $USER /etc/group`
SGROUPS=''
set -- $DATA3
ARGCNT="$#"
if \[ $ARGCNT -eq "0" \]; then
SGROUPS=$SGROUP
else
while \[ $ARGCNT -gt "0" \]; do
SGPNAME=`echo $1 | cut -d: -f1`
SGROUPS="$SGROUPS,$SGPNAME"
shift
ARGCNT="$#"
done
SGROUPS=${SGROUPS:1}
fi
echo $SNAME $HOSTNAME $SID $SGROUP $SGROUPS $SHOME $SSHELL
# echo id=$SID
# echo pgrp=$SGROUP
# echo groups=$SGROUPS
# echo home=$SHOME
# echo shell=$SSHELL
# echo gecos=$SGCOS
}
FILE="/tmp/lsuser"
UID_MIN=$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)
UID_MAX=$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)
umask 0077
echo -n >$FILE
for i in `getent passwd|cut -d : -f 1 `; do
OUT=$(fn_lsuser $i)
if (echo "$OUT"|egrep -q "(bash|zsh|nologin)") ; then
USER_UID=$(echo "$OUT" | awk '{print $3}')
if [[ $USER_UID -ge $UID_MIN && $USER_UID -le $UID_MAX ]] ; then
echo "$OUT" >>$FILE
fi ;
fi ;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment