1 |
package UML::Utils; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
require Exporter; |
7 |
our @ISA = qw(Exporter); |
8 |
|
9 |
our @EXPORT = |
10 |
qw( |
11 |
ask askCmd getDirList logAction get_sys_userid get_sys_username |
12 |
$mount |
13 |
$chmod |
14 |
$rm |
15 |
$route |
16 |
$arp |
17 |
$ifconfig |
18 |
$tunctl |
19 |
$bin_id |
20 |
$linux |
21 |
$sudo |
22 |
$ssh |
23 |
$kill |
24 |
$sh |
25 |
$sync |
26 |
$shutdown |
27 |
$poweroff |
28 |
$ps |
29 |
$uml_mconsole |
30 |
$screen |
31 |
$fuser |
32 |
$halt |
33 |
); |
34 |
|
35 |
#use UML::Config; |
36 |
my $DEBUG_STEP = $UML::Config::DEBUG_STEP; |
37 |
|
38 |
use DirHandle; |
39 |
|
40 |
our $mount = "/bin/mount"; |
41 |
our $chmod = "/bin/chmod"; |
42 |
our $rm = "/bin/rm"; |
43 |
our $route = "/sbin/route"; |
44 |
our $arp = "/sbin/arp"; |
45 |
our $ifconfig = "/sbin/ifconfig"; |
46 |
our $tunctl = "/usr/bin/tunctl"; |
47 |
our $bin_id = "/usr/bin/id"; |
48 |
our $linux = "/usr/bin/linux"; |
49 |
our $sudo = "/usr/bin/sudo"; |
50 |
our $ssh = "/usr/bin/ssh"; |
51 |
our $kill = "/bin/kill"; |
52 |
our $sh = "/bin/sh"; |
53 |
our $sync = "/bin/sync"; |
54 |
our $shutdown = "/sbin/shutdown"; |
55 |
our $poweroff = "/sbin/poweroff"; |
56 |
our $ps = "/bin/ps"; |
57 |
our $uml_mconsole = "/usr/bin/uml_mconsole"; |
58 |
our $screen = "/usr/bin/screen"; |
59 |
our $fuser = "/bin/fuser"; |
60 |
our $halt = "/sbin/halt"; |
61 |
|
62 |
|
63 |
sub logMain { |
64 |
foreach (@_) { |
65 |
if ($_) { print $_; } |
66 |
} |
67 |
} |
68 |
|
69 |
sub logResult { |
70 |
my $ls = shift; |
71 |
logMain(" ...", $ls); |
72 |
} |
73 |
|
74 |
sub logAction { |
75 |
my $ls = shift; |
76 |
logMain("\n", $ls); |
77 |
} |
78 |
|
79 |
sub doCmd { |
80 |
my $cmd = shift; |
81 |
my $res = system($cmd); |
82 |
if ($res == 0) { |
83 |
logResult("ok"); |
84 |
} else { |
85 |
logResult("failed ($res: $!)"); |
86 |
} |
87 |
} |
88 |
|
89 |
sub ask { |
90 |
my $quest = shift; |
91 |
print "$quest (y|n)"; |
92 |
my $input; |
93 |
read STDIN, $input, 2; |
94 |
if ($input =~ m/y/i) { |
95 |
return 1; |
96 |
} else { |
97 |
return 0; |
98 |
} |
99 |
} |
100 |
|
101 |
sub askCmd { |
102 |
my $quest = shift; |
103 |
my $cmd = shift; |
104 |
my $description = shift; |
105 |
if ($DEBUG_STEP) { |
106 |
#print "cmd: $cmd", "\n"; |
107 |
print "cmd: $cmd", " "; |
108 |
if (ask($quest)) { |
109 |
#print `$cmd`; |
110 |
doCmd($cmd); |
111 |
} |
112 |
} else { |
113 |
$description && ($description .= ": "); |
114 |
logAction($description); |
115 |
#`$cmd`; |
116 |
doCmd($cmd); |
117 |
} |
118 |
} |
119 |
|
120 |
|
121 |
# "global" vars used in directory-recursive-parsing |
122 |
my $dirlist_buf; |
123 |
my @dirlist_path; |
124 |
my $dirlist_base; |
125 |
|
126 |
sub entry_callback { |
127 |
|
128 |
my $entry = shift; |
129 |
|
130 |
# CHECKS |
131 |
# dont't use this: |
132 |
if ($entry eq '.' || $entry eq '..') { return; } |
133 |
|
134 |
# PREPARE |
135 |
# prepare path to current entry |
136 |
my $cur_entry = join('/', @dirlist_path, $entry); |
137 |
# prepare path to current entry (absolute) |
138 |
my $cur_entry_abs = join('/', $dirlist_base, @dirlist_path, $entry); |
139 |
|
140 |
# ENTRY |
141 |
# add current entry to buffer |
142 |
$dirlist_buf .= $cur_entry . "\n"; |
143 |
|
144 |
# (SUB-)DIRECTORY |
145 |
# check if current entry is a (sub-)directory ... |
146 |
if (-d $cur_entry_abs) { |
147 |
push @dirlist_path, $cur_entry; |
148 |
# ... and parse this (recursion here!!!) |
149 |
iterate_path($cur_entry_abs); |
150 |
pop @dirlist_path; |
151 |
} |
152 |
} |
153 |
|
154 |
sub iterate_path { |
155 |
|
156 |
my $path = shift; |
157 |
|
158 |
# create new "DirHandle"-object |
159 |
my $d = new DirHandle $path; |
160 |
if (defined $d) { |
161 |
|
162 |
# iterate through all entries in $path ($d->read) and call out entry-handler on each entry |
163 |
while (defined(my $line = $d->read)) { |
164 |
entry_callback($line); |
165 |
} |
166 |
|
167 |
undef $d; |
168 |
} |
169 |
} |
170 |
|
171 |
|
172 |
|
173 |
sub getDirList { |
174 |
|
175 |
$dirlist_base = shift; |
176 |
|
177 |
# reset vars |
178 |
$dirlist_buf = ''; |
179 |
@dirlist_path = (); |
180 |
|
181 |
# start parsing file-structure |
182 |
iterate_path($dirlist_base); |
183 |
|
184 |
# return complete list of directory-content including files and subdirs |
185 |
# entries are newline (\n) - seperated |
186 |
return $dirlist_buf; |
187 |
|
188 |
} |
189 |
|
190 |
sub get_sys_userid { |
191 |
my $username = shift; |
192 |
$username ||= ''; |
193 |
my $cmd = "$bin_id -u $username"; |
194 |
my $userid = `$cmd`; |
195 |
chomp($userid); |
196 |
return $userid; |
197 |
} |
198 |
|
199 |
sub get_sys_username { |
200 |
my $username = shift; |
201 |
$username ||= ''; |
202 |
my $cmd = "$bin_id -un $username"; |
203 |
my $userid = `$cmd`; |
204 |
chomp($userid); |
205 |
return $userid; |
206 |
} |
207 |
|
208 |
|
209 |
1; |
210 |
|