perl减轻工作量(第一个perl脚本)

#!usr/bin/perl -w
use strict;
my $vmps="vmps";
my $bl=0;
my @department= qw/ develop general /;
my $usage = <<EOU;
usage :vmpsmac -a physical_address department
       vmpsmac -d physical_address
       vmpsmac -q physical_address
EOU
########################start sub #############################################
sub del_mac{
my($filename,$kw)=(shift,shift);
open ( VMPS, "+<", $vmps) or die "Can't access file.\n";
my @lines = <VMPS>;
s/.*$kw.*//s for @lines;
seek(VMPS,0,0);
print VMPS @lines;
truncate(VMPS,tell(VMPS));
close VMPS;
}
sub add_mac{
my($filename,$kw,$dept)=(shift,shift,shift);
open ( VMPS, "+<", $vmps) or die "Can't access file.\n";
my @lines = <VMPS>;
push @lines, "address  $kw  vlan-name $dept\n";
seek(VMPS,0,0);
print VMPS @lines;
truncate(VMPS,tell(VMPS));
close VMPS;
}
sub que_mac{
my($filename,$kw)=(shift,shift);
my $bee=0;
open ( VMPS, "+<", $vmps) or die "Can't access file.\n";
my @lines = <VMPS>;
foreach (@lines){
$bee =1 if /$kw/;
}
$bee?print "$kw is already exist!\n":print "$kw is not match\n";
close VMPS;
return $bee;
}
###################### end  sub ###########################################
unless ($ARGV[0] =~ /-[adq]/){
print $usage;
exit;
}
unless ($ARGV[1] =~ /^([0-9A-F]{4}\.){2}[0-9A-F]{4}$/){
print $usage;
exit;
}
del_mac($vmps,$ARGV[1]) if $ARGV[0] eq "-d";
que_mac($vmps,$ARGV[1]) if $ARGV[0] eq "-q";
if ($ARGV[0] eq "-a"){
foreach (@department){
$bl =1 if /^$ARGV[2]$/;
}
unless ($bl){
print $usage;
exit;
}
add_mac($vmps,$ARGV[1],$ARGV[2]) if !($bl=que_mac($vmps,$ARGV[1]));
}

你可能感兴趣的:(职场,perl,休闲,vmps)