Archive for the ‘Perl’ Category

Perl: Simple Script to Find Users on a Linux/Unix/Numa System

Thursday, October 29th, 2009
#!/usr/local/bin/perl
#This program was designed to gather userid, user number
#
#The program will grab the information from /etc/passwd
#and append it to a file with the hostname echoed every
#10 lines.

use strict;

#set variables
my $line; #This is the line of the file being searched
my @userid; #This is the array to place /etc/passwd in.
my @line; #This is the array for each line
my $hostname = `hostname`; #This is the hostname for the server
my $sys_users;
my $linetwo; #This is for each line of the shadow file
my @usrlock; #array to hold shadow file
my @linetwo; #array to hold each line of the shadow file

#Let's change into the directory to make sure the file exists
chdir "/etc/" || die "Could not change directory to /etc $!";
$sys_users="/mnt/$hostname"; #This is the file the information will go to
#open the file or just die
open (FILE, "/etc/passwd") or die $!; #open the password file
@userid= ; #Place the data into an array

#close file handle
close (FILE);
open (FILE,"/etc/shadow");
@usrlock = ;
close (FILE);
open (FILE, ">> $sys_users") or die $!;
print FILE "nn$hostnamen";
#Lets process the information in the array
foreach $line(@userid){
chomp $line;
@line = split(/:/,$line, 7);
foreach $linetwo(@usrlock){
chomp $linetwo;
@linetwo = split(/:/,$linetwo,9);
chomp ($line[0],$linetwo[0]);
chomp $linetwo[1];
if ($line[0] eq $linetwo[0]){
if ($linetwo[1] =~ m/(LOCK|*LK*)/){
$linetwo[1]="LOCKED";
}
else {
$linetwo[1]="UNLOCKED";
}
print FILE "USEID:$line[0]tUSER NAME:$line[4]tLOGIN STATUS:$linetwo[1]tUSER ID:$line[2]ttGROUP:$line[3]n";
}
}
}
close (FILE);
exit();

Enjoy,

Mike Kniaziewicz