line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Slaughter::API::linux - Perl Automation Tool Helper Linux implementation |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=cut |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 SYNOPSIS |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
This module is the one that gets loaded upon Linux systems, after the generic |
12
|
|
|
|
|
|
|
API implementation. It implements the platform-specific parts of our |
13
|
|
|
|
|
|
|
primitives. |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
We also attempt to load C<Slaughter::API::Local::linux>, where site-specific primitives |
16
|
|
|
|
|
|
|
may be implemented. If the loading of this additional module fails we report no error/warning. |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=cut |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 METHODS |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
Now follows documentation on the available methods. |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=cut |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
8
|
|
|
8
|
|
24
|
use strict; |
|
8
|
|
|
|
|
9
|
|
|
8
|
|
|
|
|
198
|
|
29
|
8
|
|
|
8
|
|
23
|
use warnings; |
|
8
|
|
|
|
|
9
|
|
|
8
|
|
|
|
|
280
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
package Slaughter::API::linux; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
our $VERSION = "3.0.5"; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
8
|
|
|
8
|
|
1662
|
use Slaughter::Packages::linux; |
|
8
|
|
|
|
|
13
|
|
|
8
|
|
|
|
|
203
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head2 import |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Export all subs in this package into the main namespace. |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=cut |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub import |
54
|
|
|
|
|
|
|
{ |
55
|
|
|
|
|
|
|
|
56
|
8
|
|
|
8
|
|
31
|
no strict 'refs'; |
|
8
|
|
|
|
|
8
|
|
|
8
|
|
|
|
|
2828
|
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
59
|
8
|
|
|
8
|
|
14
|
my $caller = caller; |
60
|
|
|
|
|
|
|
|
61
|
8
|
|
|
|
|
11
|
while ( my ( $name, $symbol ) = each %{ __PACKAGE__ . '::' } ) |
|
72
|
|
|
|
|
219
|
|
62
|
|
|
|
|
|
|
{ |
63
|
64
|
100
|
|
|
|
87
|
next if $name eq 'BEGIN'; |
64
|
56
|
100
|
|
|
|
63
|
next if $name eq 'import'; |
65
|
48
|
100
|
|
|
|
98
|
next unless *{ $symbol }{ CODE }; |
|
48
|
|
|
|
|
86
|
|
66
|
|
|
|
|
|
|
|
67
|
32
|
|
|
|
|
38
|
my $imported = $caller . '::' . $name; |
68
|
32
|
|
|
|
|
24
|
*{ $imported } = \*{ $symbol }; |
|
32
|
|
|
|
|
55
|
|
|
32
|
|
|
|
|
27
|
|
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head2 InstallPackage |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
The InstallPackage primitive will allow you to install a system package. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Currently C<apt-get> and C<yum> are supported, via L<Slaughter::Packages::linux>. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=for example begin |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
foreach my $package ( qw! bash tcsh ! ) |
83
|
|
|
|
|
|
|
{ |
84
|
|
|
|
|
|
|
if ( PackageInstalled( Package => $package ) ) |
85
|
|
|
|
|
|
|
{ |
86
|
|
|
|
|
|
|
print "$package installed\n"; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
else |
89
|
|
|
|
|
|
|
{ |
90
|
|
|
|
|
|
|
InstallPackage( Package => $package ); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=for example end |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
The following parameters are available: |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=over |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=item Package [mandatory] |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
The name of the package to install. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=back |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=cut |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub InstallPackage |
109
|
|
|
|
|
|
|
{ |
110
|
0
|
|
|
0
|
1
|
|
my (%params) = (@_); |
111
|
|
|
|
|
|
|
|
112
|
0
|
|
0
|
|
|
|
my $package = $params{ 'Package' } || return; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
my $helper = Slaughter::Packages::linux->new(); |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
122
|
0
|
0
|
|
|
|
|
if ( $helper->recognised() ) |
123
|
|
|
|
|
|
|
{ |
124
|
0
|
|
|
|
|
|
$helper->installPackage( $params{ 'Package' } ); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
else |
127
|
|
|
|
|
|
|
{ |
128
|
0
|
|
|
|
|
|
print "Unknown Linux type. Packaging support not present\n"; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 PackageInstalled |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Test whether a given system package is installed. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=for example begin |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
if ( PackageInstalled( Package => "exim4-config" ) ) |
141
|
|
|
|
|
|
|
{ |
142
|
|
|
|
|
|
|
print "$package installed\n"; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=for example end |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
The following parameters are supported: |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=over 8 |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=item Package |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
The name of the package to test. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=back |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
The return value will be a 0 if not installed, or 1 if it is. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Currently C<apt-get> and C<yum> are supported, via L<Slaughter::Packages::linux>. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=cut |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub PackageInstalled |
164
|
|
|
|
|
|
|
{ |
165
|
0
|
|
|
0
|
1
|
|
my (%params) = (@_); |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
0
|
|
|
|
my $package = $params{ 'Package' } || return; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
|
172
|
0
|
|
|
|
|
|
my $helper = Slaughter::Packages::linux->new(); |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
|
177
|
0
|
0
|
|
|
|
|
if ( $helper->recognised() ) |
178
|
|
|
|
|
|
|
{ |
179
|
0
|
|
|
|
|
|
$helper->isInstalled($package); |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
else |
182
|
|
|
|
|
|
|
{ |
183
|
0
|
|
|
|
|
|
print "Unknown Linux type. Packaging support not present\n"; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head2 RemovePackage |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Remove the specified system package from the system. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=for example begin |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
if ( PackageInstalled( Package => 'telnetd' ) ) |
197
|
|
|
|
|
|
|
{ |
198
|
|
|
|
|
|
|
RemovePackage( Package => 'telnetd' ); |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=for example end |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
The following parameters are supported: |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=over 8 |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=item Package |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
The name of the package to remove. |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=back |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
Currently C<apt-get> and C<yum> are supported, via L<Slaughter::Packages::linux>. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=cut |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
sub RemovePackage |
218
|
|
|
|
|
|
|
{ |
219
|
0
|
|
|
0
|
1
|
|
my (%params) = (@_); |
220
|
|
|
|
|
|
|
|
221
|
0
|
|
0
|
|
|
|
my $package = $params{ 'Package' } || return; |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
|
226
|
0
|
|
|
|
|
|
my $helper = Slaughter::Packages::linux->new(); |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
|
231
|
0
|
0
|
|
|
|
|
if ( $helper->recognised() ) |
232
|
|
|
|
|
|
|
{ |
233
|
0
|
|
|
|
|
|
$helper->removePackage( $params{ 'Package' } ); |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
else |
236
|
|
|
|
|
|
|
{ |
237
|
0
|
|
|
|
|
|
print "Unknown Linux type. Packaging support not present\n"; |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=head2 UserCreate |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
Create a new user for the system. |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=for example begin |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
# TODO |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=for example end |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
The following parameters are required: |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=over 8 |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=item Login |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
The username to create. |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=item UID |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
The UID for the user. |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=item GID |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
The primary GID for the user. |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=back |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
You may optionally specify the GCos field to use. |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
=cut |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
sub UserCreate |
277
|
|
|
|
|
|
|
{ |
278
|
0
|
|
|
0
|
1
|
|
my (%params) = (@_); |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
|
283
|
0
|
|
|
|
|
|
foreach my $variable (qw! Login UID GID !) |
284
|
|
|
|
|
|
|
{ |
285
|
0
|
0
|
|
|
|
|
if ( !defined( $params{ $variable } ) ) |
286
|
|
|
|
|
|
|
{ |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
|
291
|
0
|
|
|
|
|
|
return ( $params{ $variable } ); |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
} |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
|
298
|
0
|
0
|
|
|
|
|
$params{ 'Gcos' } = $params{ 'Login' } if ( !$params{ 'Gcos' } ); |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
|
303
|
0
|
|
|
|
|
|
my $cmd = |
304
|
|
|
|
|
|
|
"useradd -c \"$params{ 'Gcos' }\"" . " -G adm" . |
305
|
|
|
|
|
|
|
" -g $params{ 'Login' }" . " -m" . " -u $params{ 'UID' }" . |
306
|
|
|
|
|
|
|
" -s /bin/bash" . " $params{ 'Login' }"; |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
|
309
|
0
|
|
|
|
|
|
RunCommand( Cmd => "addgroup --gid $params{ 'UID' } $params{ 'Login' }" ); |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
|
312
|
0
|
|
|
|
|
|
RunCommand( Cmd => $cmd ); |
313
|
|
|
|
|
|
|
} |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
1; |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
=head1 AUTHOR |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
Steve Kemp <steve@steve.org.uk> |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
=cut |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
=head1 LICENSE |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
Copyright (c) 2010-2015 by Steve Kemp. All rights reserved. |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
This module is free software; |
331
|
|
|
|
|
|
|
you can redistribute it and/or modify it under |
332
|
|
|
|
|
|
|
the same terms as Perl itself. |
333
|
|
|
|
|
|
|
The LICENSE file contains the full text of the license. |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
=cut |
336
|
|
|
|
|
|
|
|