#!/usr/bin/perl ################### #Packages required# ################### use strict; use List::MoreUtils qw(uniq); use Data::Dumper; use warnings; ############ #Parameters# ############ #First we define the path to the file we use #do not put any / my $dac02_location = "/dac03_scripts"; my $dac03_location = "/dac03_scripts/etc"; #name of the groups my @group = ('jai','smbuser','domadm'); #number of max user on the system my $i = 8; #name of additional machines that are in passwd but not in group my @add_machines=('dacsrv01\$', 'dacwks01\$', 'dacwks02\$', 'dacwks03\$', 'dacwks04\$', 'dacwks05\$', 'daceufo201\$', 'dacluxe02\$', 'dacluxe03\$'); #special users to also update the password for my @special_users=('root','nagios'); ########### #Functions# ########### #To parse the data and store it in a hash sub DataParsing{ my ($file, @fields) = @_; my %data = (); my $item; open(DATA_PARSING, $dac02_location."/".$file) or die "Cannot open the file!"; while(){ chomp; foreach $item (@fields){ if($_ =~ /$item/){ $data{$item}=[split('[:,]',$_)]; $data{$item.'_raw'} = $_; } } } close(DATA_PARSING); return %data; } #Clean the file group and return an array with clean data to construct the new one sub CleanFiles{ my ($file, @lines) = @_; my @data_file = (); print "Dump of data_file in function:\n"; print Dumper(@data_file); open(DATA_CLEANING, $dac03_location."/".$file) or die "Cannot open the file!"; #Store the text in an array, easier to work with while(){ chomp; push(@data_file,$_); } close(DATA_CLEANING); #initialize a value to count where we are in the array in order to #delete the lines we do not need foreach my $index (reverse 0 .. $#data_file){ foreach my $item (@lines){ if ($data_file[$index] =~ $item){ splice @data_file, $index, 1; } } } # my $i = 0; # my $item; # my $item2; # foreach $item (@data_file){ # foreach $item2 (@lines){ # if ($item =~ /$item2/){ # splice(@data_file,$i,1); # } # } # $i++ # } # foreach $item (@data_file){ # print "$item\n"; # } return @data_file; } #function to generate the new config files sub CreateConfig{ my ($file,@data) = @_; my $item; $file = $file.".new"; open(NFILE,">$file"); foreach $item (@data){ printf NFILE "$item\n"; } close(NFILE); return $file; } ########### #Instances# ########### #hash of the group with the group name as reference my %group = DataParsing('group', @group); #print "$group{jai}[2]\n"; #put the usernames in an array to get info out of passwd my $item; my @username = (); foreach $item (@group){ for ($a = $i+1; $a > 2; $a--){ if($group{$item}[$a] ne ''){ push(@username,$group{$item}[$a]) } } } #array containing all of the user that need to be added on system my @usernames = (); @usernames=uniq(@username,@add_machines); my @special_usernames = uniq(@usernames,@special_users); #hash of the user in passwd with username as reference my %passwd = DataParsing('passwd',@special_usernames); #hash of the password in shadow with username as reference my %shadow = DataParsing('shadow',@special_usernames); ######################################################## ######################################################## my @group_clean_data = CleanFiles('group',@group); my @passwd_clean_data = CleanFiles('passwd',@special_usernames); print "Print of the group_clean_data outside of function:\n"; print Dumper(@group_clean_data); print "Print of passwd_clean_data outside of function:\n"; print Dumper(@passwd_clean_data);