3 #This program is free software: you can redistribute it and/or modify
4 #it under the terms of the GNU General Public License as published by
5 #the Free Software Foundation, either version 3 of the License, or
6 #(at your option) any later version.
8 #This program is distributed in the hope that it will be useful,
9 #but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 #GNU General Public License for more details.
13 #You should have received a copy of the GNU General Public License
14 #along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # Written by Richard Hartmann in 2009, feel free to email
17 # richih dot mailinglist -at- gmail dot com with questions.
21 hostuuid - program to create, display, check /etc/hostuuid
29 use Getopt
::Long
qw(:config no_ignore_case bundling);
34 my $program = basename
($0);
36 my $file = '/etc/hostuuid';
47 B<hostuuid> [-f I<file>]
49 B<hostuuid> -c [--force] [-f I<file>]
51 echo 4f1cc27c-6c7b-4419-9db6-5b2f6a600b37 | B<hostuuid>
55 B<hostuuid> creates, displays and verifies the contents of /etc/hostuuid or any other file.
56 It will also read anything on STDIN and verify if it's a UUID.
62 =item B<-f|--file> I<filename>
64 The I<filename> B<hostuuid> will read from/write to.
68 Force creation of new UUID.
77 B<hostuuid> will still exit with proper return codes for you to script with.
81 Save this UUID to I<filename>.
82 Defaults to /etc/hostuuid
86 Do not listen on STDIN.
87 Needed to work around a bug in debconf.
95 Print version information.
113 Richard Hartmann <richih.mailinglist@gmail.com>
118 'file|f=s' => \
$file,
120 'create|c' => \
$create_uuid,
121 'input=s' => \
$input,
122 'quiet|q' => \
$quiet,
123 'nostdin' => \
$no_stdin,
124 'help|h|?' => \
$print_help,
125 'version' => \
$print_version,
126 ) or print_help
(255);
128 sub print_version
() {
131 $program: version $VERSION
133 This program is released under GPLv3
134 Written by Richard Hartmann for Debian and Grml
141 my ($exit_code) = @_;
146 -f, --file : file to read from/write to. Defaults to /etc/hostuuid
147 --force : force creation of new UUID
148 -c, --create : create & save UUID
149 -q, --quiet : Don't print anything
150 --input : Save UUID to file. Defaults to /etc/hostuuid
151 --nostdin : Do not listen on STDIN
152 -h, -?, --help : print this help
153 --version : print version
160 my $uuid = find_hostuuid
();
161 if ($uuid && !$force) {
162 print STDERR
"$program: Found UUID '$uuid' in '$file'. Aborting!\n" unless $quiet;
165 save_uuid
(create_UUID_as_string
(UUID_V4
));
169 sub find_hostuuid
() {
170 return 0 unless ((-e
$file && $create_uuid) || !$create_uuid);
171 open (F
, "<", "$file") or do {
172 die "$program: Could not open file: '$file'\: $!\n" unless $quiet;
175 my $uuid = check_uuid
(<F
>);
182 return $1 if ($string =~ /^([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})$/i);
188 unless (check_uuid
($uuid)) {
189 print "$uuid is NOT a valid UUID\n" unless $quiet;
192 open (F
, ">", "$file") or do {
193 die "$program: Could not open file: '$file'\: $!\n" unless $quiet;
196 print "$program\: Stored UUID '$uuid' in '$file'\n" unless $quiet;
203 unless ($no_stdin || -t STDIN
) { ## we need to test for $no_stdin cause debconf makes us hang, otherwise
204 my $uuid = check_uuid
(<STDIN
>);
206 print "$uuid is a valid UUID\n" unless $quiet;
209 print "NOT a valid UUID\n" unless $quiet;
213 print_version
() if $print_version;
214 print_help
(0) if $print_help;
215 save_uuid
($input) if $input;
216 create_uuid
() if $create_uuid;
218 my $uuid = find_hostuuid
();
220 print "$uuid\n" unless $quiet;
223 print STDERR
"$program: Could not find valid UUID in '$file'\n" unless $quiet;