perl系统管理_系统管理员的Perl技巧

perl系统管理

您是否知道Perl是系统管理员的绝佳编程语言? Perl与平台无关,因此您可以在不同的操作系统上执行操作,而无需重写脚本。 Perl中的脚本编写既快捷又容易,其可移植性使您的脚本非常有用。 这里有一些例子,只是为了让您的创意源源不断!

重命名一堆文件

假设您需要重命名目录中的一堆文件。 在这种情况下,我们有一个充满.xml文件的目录,我们想将它们全部重命名为.html 。 十分简单!



   
   
#!/usr/bin/perl
use strict ;
use warnings ;

foreach my $file ( glob "*.xml" ) {
    my $new = substr ( $file , 0 , - 3 ) . "html" ;
    rename $file , $new ;
}

然后只需cd到需要进行更改的目录,然后运行脚本。 如果需要定期运行,可以将其放在cron作业中,并且可以轻松地增强它以接受参数。

说到接受参数,让我们看一个脚本。

创建一个Linux用户帐户

假设您需要在系统上定期创建Linux用户帐户,并且用户名的格式为名字的名字/姓氏,这在许多企业中很常见。 (当然,这是一个好主意,直​​到您让John Smith和Jane Smith在同一家公司工作–或希望John在两个不同部门兼职时都拥有两个帐户。但是让我感到幽默,好吗? )每个用户帐户都必须根据其部门位于一个组中,并且主目录的格式为/ home / <部门> / <用户名> 。 让我们看一个脚本来做到这一点:



   
   
#!/usr/bin/env perl
use strict ;
use warnings ;

my $adduser = '/usr/sbin/adduser' ;

use Getopt :: Long qw ( GetOptions ) ;

# If the user calls the script with no parameters,
# give them help!

if ( not @ ARGV ) {
    usage ( ) ;
}

# Gather our options; if they specify any undefined option,
# they'll get sent some help!

my %opts ;
GetOptions ( \%opts ,
    'fname=s' ,
    'lname=s' ,
    'dept=s' ,
    'run' ,
) or usage ( ) ;

# Let's validate our inputs. All three parameters are
# required, and must be alphabetic.
# You could be clever, and do this with a foreach loop,
# but let's keep it simple for now.

if ( not $opts { fname } or $opts { fname } !~ /^[a-zA-Z]+$/ ) {
    usage ( "First name must be alphabetic" ) ;
}
if ( not $opts { lname } or $opts { lname } !~ /^[a-zA-Z]+$/ ) {
    usage ( "Last name must be alphabetic" ) ;
}
if ( not $opts { dept } or $opts { dept } !~ /^[a-zA-Z]+$/ ) {
    usage ( "Department must be alphabetic" ) ;
}

# Construct the username and home directory

my $username = lc ( substr ( $opts { fname } , 0 , 1 ) . $opts { lname } ) ;
my $home     = "/home/$opts{dept}/$username" ;

# Show them what we've got ready to go.

print "Name:           $opts{fname} $opts{lname} \n " ;
print "Username:       $username \n " ;
print "Department:     $opts{dept} \n " ;
print "Home directory: $home \n \n " ;

# use qq() here, so that the quotes in the --gecos flag
# get carried into the command!

my $cmd = qq ( $adduser -- home $home -- ingroup $opts { dept } \\
-- gecos "$opts{fname} $opts{lname}" $username ) ;

print "$cmd \n " ;
if ( $opts { run } ) {
    system $cmd ;
} else {
    print "You need to add the --run flag to actually execute \n " ;
}

sub usage {
    my ( $msg ) = @_ ;
    if ( $msg ) {
        print "$msg \n \n " ;
    }
    print "Usage: $0 --fname FirstName --lname LastName --dept Department --run \n " ;
    exit ;
}

与以前的脚本一样,有很多增强的机会,但是,完成此任务可能只需要类似的内容。

还有一个,只是为了好玩!

更改目录树中每个Perl源文件中的版权文本

现在,我们将尝试进行大规模编辑。 假设您有一个充满代码的目录,并且每个文件中都有一个版权声明。 (Rich Bowen写了一篇很棒的文章, 版权声明在开源代码中泛滥,在几年前,它讨论了开源代码中版权声明的智慧。这是一本不错的书,我强烈推荐它。但是我还是很幽默。 )您想要更改目录树中每个文件中的文本。 File::FindFile::Slurp是您的朋友!



   
   
#!/usr/bin/perl
use strict ;
use warnings ;

use File :: Find qw ( find ) ;
use File :: Slurp qw ( read_file write_file ) ;

# If the user gives a directory name, use that. Otherwise,
# use the current directory.

my $dir = $ARGV [ 0 ] || '.' ;

# File::Find::find is kind of dark-arts magic.
# You give it a reference to some code,
# and a directory to hunt in, and it will
# execute that code  on every file in the
# directory, and all subdirectories. In this
# case, \&change_file is the reference
# to our code, a subroutine.  You could, if
# what you wanted to do was really short,
# include it in a { } block instead. But doing
# it this way is nice and readable.

find ( \&change_file , $dir ) ;
 
sub change_file {
    my $name = $_ ;

    # If the file is a directory, symlink, or other
    # non-regular file, don't do anything

    if ( not - f $name ) {
        return ;
    }
    # If it's not Perl, don't do anything.

    if ( substr ( $name , - 3 ) ne ".pl" ) {
        return ;
    }
    print "$name \n " ;

    # Gobble up the file, complete with carriage
    # returns and everything.
    # Be wary of this if you have very large files
    # on a system with limited memory!

    my $data = read_file ( $name ) ;

    # Use a regex to make the change. If the string appears
    # more than once, this will change it everywhere!

    $data =~ s/Copyright Old/Copyright New/g ;

    # Let's not ruin our original files

    my $backup = "$name.bak" ;
    rename $name , $backup ;
    write_file ( $name , $data ) ;

    return ;
}

由于Perl的可移植性,您可以在Windows系统和Linux系统上使用此脚本-由于底层的Perl解释器代码,它可以正常工作。 在我们上面的创建帐户代码中,该代码不是可移植的,而是特定于Linux的,因为它使用Linux命令(例如adduser)

以我的经验,我发现将这些东西的Git存储库放置在某个地方,可以在正在使用的每个新系统上克隆,这很有用。 随着时间的推移,您会想到对代码进行更改以增强功能,或者添加新的脚本,而Git可以帮助您确保所有工具和技巧在所有系统上均可用。

我希望这些小脚本能给您一些想法,让您知道如何使用Perl来简化系统管理工作。 除了这些较长的脚本之外,还请查看Perl单行代码的精彩列表,并链接到 Mischa Peterson汇编的其他Perl魔术

翻译自: https://opensource.com/life/16/7/perl-tricks-system-administrators

perl系统管理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值