# mkGrpRpt prepares a report listing members of all groups on the # current domain. # Created 19-mar-1998 by fhb. # # Way slow! Also need to exclude certain groups from report. require "ctime.pl"; $bDebug = 1; $sAppName = "mkGrpRpt"; $sDate = &ctime(time); $sFullName = ""; $sRptFile = ">NetGrpRpt.txt"; $sTempFile = "t.t"; $sTmp = ""; $sToday = substr($sDate,4); $nTotGrps = 0; $sUser = ""; $sUserFile = "Users.lis"; $sVersion = "0.1"; MAIN: print "$sAppName version $sVersion starting at $sToday\n"; # Read raw output of NET GROUP command into temp file: $sCmd = sprintf("net group /dom >%s",$sTempFile); if ($bDebug) { print "dbg> $sCmd\n"; } system($sCmd); # Parse temp file into a list of groups: @lGroups = (); open(FTEMP,$sTempFile) || die "Can't open file $sTempFile: $!\n"; $bEndOfHeader = 0; while () { chop $_; if (/^-----/) { $bEndOfHeader = 1; next; } if (!$bEndOfHeader) { next; } if (/^\*(.*)/) { # find lines beginning w '*' # ($g1,$g2,$g3) = split; $g1 = substr($_,1,22); $g2 = substr($_,27,22); $g3 = substr($_,53); if ($g1 ne "") { Trim($g1); push(@lGroups,$g1); ++$nTotGrps; } if ($g2 ne "") { Trim($g2); push(@lGroups,$g2); ++$nTotGrps; } if ($g3 ne "") { Trim($g3); push(@lGroups,$g3); ++$nTotGrps; } } } close(FTEMP); # Open our report file & print header: open(FRPT,$sRptFile) || die "Can't open report file $sRptFile: $!\n"; print FRPT "network groups ($nTotGrps): $sToday\n\n"; print "Found $nTotGrps groups\n"; # Get members of each group (via raw output of NET GROUP) into temp file: foreach $_ (@lGroups) { if (ExcludeThisGroup($_)) { next; } print FRPT "\nGroup $_:\n\n"; print "\nProcessing group $_"; $sCmd = sprintf("net group \"%s\" /dom>%s",$_,$sUserFile); if ($bDebug) { print "dbg> $sCmd\n"; } system($sCmd); # Format the raw output into our report: $nTotUsers = 0; @lUsers = (); open(FULIST,$sUserFile) || print "Can't open file $sUserFile: $!\n"; $bEndOfHeader = 0; while () { chop $_; if (/^-----/) { $bEndOfHeader = 1; next; } if (!$bEndOfHeader) { next; } if (/^The command/) { # that's all folks last; } ($u1,$u2,$u3) = split; if ($u1 ne "") { $sUser = $u1; Trim($u1); ProcessUser(); } if ($u2 ne "") { $sUser = $u2; Trim($u2); ProcessUser(); } if ($u3 ne "") { $sUser = $u3; Trim($u3); ProcessUser(); } } close(FULIST); } print FRPT "\n*** End of report ***\n"; close(FRPT); unlink($sTempFile); print "\nCreated report file $sRptFile\n"; print "$sAppName complete: processed $nTotUsers users in $nTotGrps groups.\n"; ###### subroutines ####### sub ExcludeThisGroup { $bIgnore = 0; $s = $_[0]; @lIgnore = ("Domain Admins","Domain Guests","Domain Users","thisGroup","Crazy Users"); foreach $sExclude (@lIgnore) { if ($s eq $sExclude) { $bIgnore = 1; last; } } return $bIgnore; } sub GetUserInfo { $sFullName = ""; $sCmd = sprintf("net user %s /dom >%s",$sUser,$sTempFile); # capture output system($sCmd); open(FTEMP,$sTempFile) || die "Can't open file $sTempFile: $!\n"; while () { chop $_; if (/^Full Name/) { $sFullName = substr($_,29); last; } } close(FTEMP); if ($bDebug) { print "dbg> full name: $sFullName\n"; } else { print "."; }; } sub PrintUserRec { $sLine = sprintf("\"%s\",\"%s\"\n",$sUser,$sFullName); print FRPT $sLine; } sub ProcessUser { GetUserInfo(); PrintUserRec(); ++$nTotUsers; } sub Trim { $s = $_[0]; # if ($bDebug) { print "dbg> untrimmed = \"$s\"\n"; } $nLen = length($_[0]); $bKeepGoing = 1; while ($bKeepGoing) { $ch = substr($s,--$nLen,1); if ($ch eq " ") { chop ($_[0]); # $n = length($s); # if ($bDebug) { print "dbg> chopped it! (length = $n, ch = \'$ch\')\n"; } } else { $bKeepGoing = 0; # if ($bDebug) { print "dbg> outta here!\n"; } } } # if ($bDebug) { print "dbg> trimmed = \"$s\""; } }