File: test.pl

package info (click to toggle)
libparse-dmidecode-perl 0.03-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 336 kB
  • sloc: perl: 1,150; makefile: 4
file content (93 lines) | stat: -rw-r--r-- 2,423 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl -w

#BEGIN { chdir '../' if -d '../examples/'; }

use strict;
use lib qw(./lib ../lib);
use Getopt::Std qw(getopts);
use Fcntl qw(:mode);
use Parse::DMIDecode;
use Parse::DMIDecode::Constants qw(@TYPES);

# Get some command line options
my $opts = {};
Getopt::Std::getopts('f:g:Kk:hV',$opts);
$opts->{g} ||= 'processor';
display_help(),exit if defined $opts->{h};

my $decoder = new Parse::DMIDecode;
my $dmidecode = '/usr/sbin/dmidecode';
my @stat = stat($dmidecode);

# If we've been given a dmidecode output file, parse that
if ($opts->{f}) {
	die "File '$opts->{f}' does not exist.\n" unless -f $opts->{f};
	$decoder->parse(`cat $opts->{f}`);

# If we're root, or dmidecode is setuid root, then probe
} elsif ($> || ($stat[4] == 0 && $stat[2] & S_ISUID)) {
	$decoder->probe;

# Otherwise run dmidecode with sudo
} else {
	$decoder->parse(qx(sudo $dmidecode));
}

# Print all of the available keywords
if (defined $opts->{K}) {
	if (defined $opts->{V}) {
		for my $keyword ($decoder->keywords) {
			my $value = $decoder->keyword($keyword);
			$value = '' unless defined $value;
			printf("Keyword '%s' => '%s'\n",
					$keyword,
					(ref($value) eq 'ARRAY' ? join(', ',@{$value}) : $value)
				);
		}
	} else {
		print join("\n",$decoder->keywords)."\n";
	}
	exit;

# Print just one keyword
} elsif ($opts->{k}) {
	printf("Keyword '%s' => '%s'\n",
			$opts->{k},
			$decoder->keyword($opts->{k})
		);
	exit;
}

# Print some information about specific structure handles
for my $handle ($decoder->get_handles( group => $opts->{g} )) {
	printf(">>> Found handle at %s (%s):\n >> Description: %s\n >> Keywords: %s\n%s\n",
			$handle->address,
			$TYPES[$handle->dmitype],
			$handle->description,
			join(', ',$handle->keywords),
			$handle->raw
		);
	if (defined $opts->{V}) {
		for my $keyword ($handle->keywords) {
			my $value = $handle->keyword($keyword);
			$value = '' unless defined $value;
			printf("  > Keyword '%s' => '%s'\n",
					$keyword,
					(ref($value) eq 'ARRAY' ? join(', ',@{$value}) : $value)
				);
		}
	}
	print "\n";
}

sub display_help {
	print qq{Syntax: $0 [-h] [-V] [-K|-k <keyword>|-g <group>] [-f <filename>]
    -h              Display this help
    -K              List all valid keywords
    -V              Print more verbose output
    -g <group>      Display handle information match <group> group
    -f <filename>   Parse dmidecode information from <filename>
};
}