我已经用perl创建了一个模块,但我不知道如何使用它
下面是我创建的模块:我已经用perl创建了一个模块,但我不知道如何使用它,perl,module,subroutine,Perl,Module,Subroutine,下面是我创建的模块: #!/usr/bin/perl use warnings; use strict; package Module1; my @user; my $count = 0; my @test; my @last =qx(last); my @logins; my %logins_by_user; my @UserDiskUsage; #my $pass =qx(sudo passwd -S @user); #Opens the file /etc/passwd and p
#!/usr/bin/perl
use warnings;
use strict;
package Module1;
my @user;
my $count = 0;
my @test;
my @last =qx(last);
my @logins;
my %logins_by_user;
my @UserDiskUsage;
#my $pass =qx(sudo passwd -S @user);
#Opens the file /etc/passwd and puts the users with an uid over 1000 but less that 65000 into an array.
sub userloggins {
open( my $passwd, "<", "/etc/passwd") or die "/etc/passwd failed to open.\n";
while (my $lines = <$passwd>) {
my @splitarray = split(/\:/, $lines );
if( $splitarray[2] >= 1000 && $splitarray[2] < 65000) {
$user[$count] =$splitarray[0];
#print "$user[$count]\n";
$count++;
}
}
close $passwd;
#Counts how many times each user has logged in and stores them in a hash, then sorts them
for my $user (@user) {
$logins_by_user{$user} = grep /^\Q$user\E /, `last '$user'`;
}
for my $user (
sort { $logins_by_user{$b} <=> $logins_by_user{$a} || $a cmp $b } @user
) {
print("$user: $logins_by_user{$user}\n");
}
}
userloggins();
#del 2, monitor user password age
print "-------------------------------------------------------\n";
PasswordAge();
sub PasswordAge {
my $days = 10;
my $currentdate = (qx(date +%s)) / 86400;
for my $i (0...$#user) {
my @shadowDays = qx(sudo grep $user[$i] /etc/shadow | cut -d: -f3);
if ($shadowDays[0] < ($currentdate - $days)){
print ("User $user[$i] has not changed their password in $days days.\n");
}
#print "$currentdate\n";
#print "$user Has not changed their password since $splitpass\n";
}
}
#/usr/bin/perl
使用警告;
严格使用;
封装模块1;
我的@user;
我的$count=0;
我的测试;
my@last=qx(last);
我的@logins;
我的用户登录次数百分比;
我的@UserDiskUsage;
#my$pass=qx(sudopasswd-S@user);
#打开文件/etc/passwd,将uid大于1000但小于65000的用户放入一个数组中。
子用户登录{
打开(my$passwd),您所问问题的答案是:
- 将模块另存为“Module1.pm”
使用模块1;
- 在脚本中:
Module1::PasswordAge();
然而,在理解如何导入和加载模块之前,可能听起来有点自命不凡(例如,作为创建模块的过程),这可能是值得一读的,虽然不是很难,但这有点不寻常。您可能需要阅读Perl模块并查看一些示例。从创建文件开始>MyModule.pm
,将您的passwordAge
子文件存储在那里,然后在同一文件夹中创建一个文件script.pl
,然后使用MyModule
并使用MyModule::passwordAge($days)测试您的功能
。但是有很多教程可以更好地解释这一点。从小事做起,逐个构建功能。