根据我在另一篇文章How to exclude a list of full directory paths in find command on Solaris 中的发现和建议,我决定编写这个脚本的 Perl 版本,看看如何优化它以比原生 find 命令运行得更快. 到目前为止,结果令人印象深刻!
此脚本的目的是报告 Unix 系统上所有未拥有的文件和目录,以确保审计合规性。该脚本必须接受要排除的目录和文件列表(通过完整路径或通配符名称),并且必须尽可能少地占用处理能力。它可以在我们(我工作的公司)支持的数百个 Unix 系统上运行,并且能够在所有这些 Unix 系统上运行(多操作系统、多平台:AIX、HP-UX、Solaris 和 Linux)无需我们先安装或升级任何东西。换句话说,它必须与我们可以在所有系统上期望的标准库和二进制文件一起运行。
我还没有让脚本参数感知,所以所有参数都硬编码在脚本中。我计划最后提出以下论点,并且可能会使用 getopts 来做到这一点:
-d = comma delimited list of directories to exclude by path name
-w = comma delimited list of directories to exclude by basename or wildcard
-f = comma delimited list of files to exclude by path name
-i = comma delimited list of files to exclude by basename or wildcard
-t:list|count = Defines the type of output I want to see (list of all findinds, or summary with count per directory)
这是我到目前为止所做的来源:
#! /usr/bin/perl
use strict;
use File::Find;
# Full paths of directories to prune
my @exclude_dirs = ('/dev','/proc','/home');
# Basenames or wildcard names of directories I want to prune
my $exclude_dirs_wildcard = '.svn';
# Full paths of files I want to ignore
my @exclude_files = ('/tmp/test/dir3/.svn/svn_file1.txt','/tmp/test/dir3/.svn/svn_file2.txt');
# Basenames of wildcard names of files I want to ignore
my $exclude_files_wildcard = '*.tmp';
my %dir_globs = ();
my %file_globs = ();
# Results will be sroted in this hash
my %found = ();
# Used for storing uid's and gid's present on system
my %uids = ();
my %gids = ();
# Callback function for find
sub wanted {
my $dir = $File::Find::dir;
my $name = $File::Find::name;
my $basename = $_;
# Ignore symbolic links
return if -l $name;
# Search for wildcards if dir was never searched before
if (!exists($dir_globs{$dir})) {
@{$dir_globs{$dir}} = glob($exclude_dirs_wildcard);
}
if (!exists($file_globs{$dir})) {
@{$file_globs{$dir}} = glob($exclude_files_wildcard);
}
# Prune directory if present in exclude list
if (-d $name && in_array(\@exclude_dirs, $name)) {
$File::Find::prune = 1;
return;
}
# Prune directory if present in dir_globs
if (-d $name && in_array(\@{$dir_globs{$dir}},$basename)) {
$File::Find::prune = 1;
return;
}
# Ignore excluded files
return if (-f $name && in_array(\@exclude_files, $name));
return if (-f $name && in_array(\@{$file_globs{$dir}},$basename));
# Check ownership and add to the hash if unowned (uid or gid does not exist on system)
my ($dev,$ino,$mode,$nlink,$uid,$gid) = stat($name);
if (!exists $uids{$uid} || !exists($gids{$gid})) {
push(@{$found{$dir}}, $basename);
} else {
return
}
}
# Standard in_array perl implementation
sub in_array {
my ($arr, $search_for) = @_;
my %items = map {$_ => 1} @$arr;
return (exists($items{$search_for}))?1:0;
}
# Get all uid's that exists on system and store in %uids
sub get_uids {
while (my ($name, $pw, $uid) = getpwent) {
$uids{$uid} = 1;
}
}
# Get all gid's that exists on system and store in %gids
sub get_gids {
while (my ($name, $pw, $gid) = getgrent) {
$gids{$gid} = 1;
}
}
# Print a list of unowned files in the format PARENT_DIR,BASENAME
sub print_list {
foreach my $dir (sort keys %found) {
foreach my $child (sort @{$found{$dir}}) {
print "$dir,$child\n";
}
}
}
# Prints a list of directories with the count of unowned childs in the format DIR,COUNT
sub print_count {
foreach my $dir (sort keys %found) {
print "$dir,".scalar(@{$found{$dir}})."\n";
}
}
# Call it all
&get_uids();
&get_gids();
find(\&wanted, '/');
print "List:\n";
&print_list();
print "\nCount:\n";
&print_count();
exit(0);
如果您想在您的系统上测试它,只需使用通用文件创建一个测试目录结构,使用您为此创建的测试用户chown 整个树,然后删除该用户。
我会接受你能给我的任何提示、提示或建议。
快乐阅读!