#!/usr/local/bin/perl $directory = $ARGV[0]; if ($directory eq "") { print "usage: clean_home_dirs.pl \n"; exit; } if (!opendir(DIRECTORY, $directory)) { print "unable to open $directory\n"; exit; } @all_directories = readdir(DIRECTORY); closedir(DIRECTORY); open(PASSWD, "/etc/master.passwd") || die "couldn't open passwd file (1): $!"; @passwd = ; close(PASSWD); foreach $i (0 .. $#all_directories) { next if (@all_directories[$i] eq ".") ; next if (@all_directories[$i] eq "..") ; $username = @all_directories[$i] ; $original_username = $username ; $home_dir = (getpwnam($username))[7]; $shell = (getpwnam($username))[8]; if (($shell eq "") || ($home_dir eq "")) { $username = "#" . $username ; foreach $i (0 .. $#passwd) { ($who, $p, $u, $g, $cl, $ch, $e, $ge, $hd, $sh, $r) = split(/:/, $passwd[$i], 11); if ($username eq $who) { $shell = $sh; chop $shell; $home_dir = $hd; } } } if (($shell eq "X") && ($directory . $original_username eq $home_dir)) { print "removed $original_username $home_dir $shell\n" ; `rm -r $home_dir` ; `rm -r /var/mail/$original_username` ; # exit ; } } exit ;