Je ne comprends pas pourquoi lorsque je fais apelle deux fois à la fonction CleanFiles, les datas du deuxième call sont ajoutées aux data du premier call. Ce qui m'étonne encore plus c'est que le problème ne se produit pas pour la fonction précédente (DataParsing).

Est-ce que quelqu'un pourrait me donner un coup de main avec ce script?

Merci d'avance

Le code est en dessous (soyez gentil, cela ne fait que 2 jours que je code en Perl )
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
 
 
#!/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(<DATA_PARSING>){
        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(<DATA_CLEANING>){
        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);
Et voice l'output de ce code:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
 
 
Dump of data_file in function:
Dump of data_file in function:
Print of the group_clean_data outside of function:
$VAR1 = 'root:x:0:';
$VAR2 = 'daemon:x:1:';
$VAR3 = 'bin:x:2:';
$VAR4 = 'sys:x:3:';
$VAR5 = 'adm:x:4:';
$VAR6 = 'tty:x:5:';
$VAR7 = 'disk:x:6:';
$VAR8 = 'lp:x:7:';
$VAR9 = 'mail:x:8:';
$VAR10 = 'news:x:9:';
$VAR11 = 'uucp:x:10:';
$VAR12 = 'man:x:12:';
$VAR13 = 'proxy:x:13:';
$VAR14 = 'kmem:x:15:';
$VAR15 = 'dialout:x:20:reyndce';
$VAR16 = 'fax:x:21:';
$VAR17 = 'voice:x:22:';
$VAR18 = 'cdrom:x:24:reyndce';
$VAR19 = 'floppy:x:25:reyndce';
$VAR20 = 'tape:x:26:';
$VAR21 = 'sudo:x:27:';
$VAR22 = 'audio:x:29:reyndce';
$VAR23 = 'dip:x:30:';
$VAR24 = 'www-data:x:33:apache';
$VAR25 = 'backup:x:34:';
$VAR26 = 'operator:x:37:';
$VAR27 = 'list:x:38:';
$VAR28 = 'irc:x:39:';
$VAR29 = 'src:x:40:';
$VAR30 = 'gnats:x:41:';
$VAR31 = 'shadow:x:42:';
$VAR32 = 'utmp:x:43:';
$VAR33 = 'video:x:44:reyndce';
$VAR34 = 'sasl:x:45:';
$VAR35 = 'plugdev:x:46:reyndce';
$VAR36 = 'staff:x:50:';
$VAR37 = 'games:x:60:';
$VAR38 = 'users:x:100:';
$VAR39 = 'nogroup:x:65534:';
$VAR40 = 'crontab:x:101:';
$VAR41 = 'Debian-exim:x:102:';
$VAR42 = 'ssh:x:103:';
$VAR43 = 'reyndce:x:1000:';
$VAR44 = 'mysql:x:104:';
$VAR45 = 'ssl-cert:x:105:';
$VAR46 = 'postfix:x:106:';
$VAR47 = 'postdrop:x:107:';
$VAR48 = 'nagios:x:1001:';
$VAR49 = 'nagcmd:x:1002:nagios,www-data';
$VAR50 = 'bind:x:108:';
$VAR51 = 'ntp:x:109:';
$VAR52 = 'apache:x:72:';
$VAR53 = 'slocate:x:110:';
Print of passwd_clean_data outside of function:
$VAR1 = 'daemon:x:1:';
$VAR2 = 'bin:x:2:';
$VAR3 = 'sys:x:3:';
$VAR4 = 'adm:x:4:';
$VAR5 = 'tty:x:5:';
$VAR6 = 'disk:x:6:';
$VAR7 = 'lp:x:7:';
$VAR8 = 'mail:x:8:';
$VAR9 = 'news:x:9:';
$VAR10 = 'uucp:x:10:';
$VAR11 = 'man:x:12:';
$VAR12 = 'proxy:x:13:';
$VAR13 = 'kmem:x:15:';
$VAR14 = 'fax:x:21:';
$VAR15 = 'voice:x:22:';
$VAR16 = 'tape:x:26:';
$VAR17 = 'sudo:x:27:';
$VAR18 = 'dip:x:30:';
$VAR19 = 'www-data:x:33:apache';
$VAR20 = 'backup:x:34:';
$VAR21 = 'operator:x:37:';
$VAR22 = 'list:x:38:';
$VAR23 = 'irc:x:39:';
$VAR24 = 'src:x:40:';
$VAR25 = 'gnats:x:41:';
$VAR26 = 'shadow:x:42:';
$VAR27 = 'utmp:x:43:';
$VAR28 = 'sasl:x:45:';
$VAR29 = 'staff:x:50:';
$VAR30 = 'games:x:60:';
$VAR31 = 'users:x:100:';
$VAR32 = 'nogroup:x:65534:';
$VAR33 = 'crontab:x:101:';
$VAR34 = 'Debian-exim:x:102:';
$VAR35 = 'ssh:x:103:';
$VAR36 = 'mysql:x:104:';
$VAR37 = 'ssl-cert:x:105:';
$VAR38 = 'postfix:x:106:';
$VAR39 = 'postdrop:x:107:';
$VAR40 = 'bind:x:108:';
$VAR41 = 'ntp:x:109:';
$VAR42 = 'apache:x:72:';
$VAR43 = 'slocate:x:110:';
$VAR44 = 'daemon:x:1:1:daemon:/usr/sbin:/bin/sh';
$VAR45 = 'bin:x:2:2:bin:/bin:/bin/sh';
$VAR46 = 'sys:x:3:3:sys:/dev:/bin/sh';
$VAR47 = 'sync:x:4:65534:sync:/bin:/bin/sync';
$VAR48 = 'games:x:5:60:games:/usr/games:/bin/sh';
$VAR49 = 'man:x:6:12:man:/var/cache/man:/bin/sh';
$VAR50 = 'lp:x:7:7:lp:/var/spool/lpd:/bin/sh';
$VAR51 = 'mail:x:8:8:mail:/var/mail:/bin/sh';
$VAR52 = 'news:x:9:9:news:/var/spool/news:/bin/sh';
$VAR53 = 'uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh';
$VAR54 = 'proxy:x:13:13:proxy:/bin:/bin/sh';
$VAR55 = 'www-data:x:33:33:www-data:/var/www:/bin/sh';
$VAR56 = 'backup:x:34:34:backup:/var/backups:/bin/sh';
$VAR57 = 'list:x:38:38:Mailing List Manager:/var/list:/bin/sh';
$VAR58 = 'irc:x:39:39:ircd:/var/run/ircd:/bin/sh';
$VAR59 = 'gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh';
$VAR60 = 'nobody:x:65534:65534:nobody:/nonexistent:/bin/sh';
$VAR61 = 'Debian-exim:x:100:102::/var/spool/exim4:/bin/false';
$VAR62 = 'statd:x:101:65534::/var/lib/nfs:/bin/false';
$VAR63 = 'identd:x:102:65534::/var/run/identd:/bin/false';
$VAR64 = 'sshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin';
$VAR65 = 'mysql:x:103:104:MySQL Server,,,:/var/lib/mysql:/bin/false';
$VAR66 = 'postfix:x:105:106::/var/spool/postfix:/bin/false';
$VAR67 = 'bind:x:106:108::/var/cache/bind:/bin/false';
$VAR68 = 'ntp:x:107:109::/home/ntp:/bin/false';