File: amttool

package info (click to toggle)
amtterm 1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 348 kB
  • ctags: 542
  • sloc: ansic: 2,533; perl: 328; makefile: 10
file content (408 lines) | stat: -rwxr-xr-x 10,565 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/usr/bin/perl
use strict;
use warnings;
use SOAP::Lite;
#use SOAP::Lite +trace => 'all';

my $amt_host = shift;
my $amt_port = "16992";
my $amt_proto = 'http';

if ($amt_host =~ /([^:]+):(\d+)/) {
  $amt_host = $1;
  $amt_port = $2;
  if ($amt_port == 16993) {
    $amt_proto = 'https';
  }
}

$main::amt_user = "admin";
$main::amt_pass = $ENV{'AMT_PASSWORD'};
my $amt_debug = 0;
$amt_debug = $ENV{'AMT_DEBUG'} if defined($ENV{'AMT_DEBUG'});

my $amt_command = shift;
$amt_command = "info" if !defined($amt_command);
my $amt_arg = shift;

my $amt_version;

#############################################################################
# data

my @ps = ("S0", "S1", "S2", "S3", "S4", "S5 (soft-off)", "S4/S5", "Off");
my %rcc = (
	"reset"      => "16",
	"powerup"    => "17",
	"powerdown"  => "18",
	"powercycle" => "19",
);

my %rccs = (
	"nop"        => "0",
	"pxe"	     => "1",
	"hd"         => "2",
	"hdsafe"     => "3",
	"diag"       => "4",
	"cd"         => "5",
);
my %rccs_oem = (
	"bios"       => 0xc1,
);

# incomplete list
my %pt_status = (
	0x0  =>  "success",
	0x1  =>  "internal error",
	0x3  =>  "invalid pt_mode",
	0xc  =>  "invalid name",
	0xf  =>  "invalid byte_count",
	0x10  =>  "not permitted",
	0x17  =>  "max limit_reached",
	0x18  =>  "invalid auth_type",
	0x1a  =>  "invalid dhcp_mode",
	0x1b  =>  "invalid ip_address",
	0x1c  =>  "invalid domain_name",
	0x20  =>  "invalid provisioning_state",
	0x22  =>  "invalid time",
	0x23  =>  "invalid index",
	0x24  =>  "invalid parameter",
	0x25  =>  "invalid netmask",
	0x26  =>  "flash write_limit_exceeded",
	0x800  =>  "network if_error_base",
	0x801  =>  "unsupported oem_number",
	0x802  =>  "unsupported boot_option",
	0x803  =>  "invalid command",
	0x804  =>  "invalid special_command",
	0x805  =>  "invalid handle",
	0x806  =>  "invalid password",
	0x807  =>  "invalid realm",
	0x808  =>  "storage acl_entry_in_use",
	0x809  =>  "data missing",
	0x80a  =>  "duplicate",
	0x80b  =>  "eventlog frozen",
	0x80c  =>  "pki missing_keys",
	0x80d  =>  "pki generating_keys",
	0x80e  =>  "invalid key",
	0x80f  =>  "invalid cert",
	0x810  =>  "cert key_not_match",
	0x811  =>  "max kerb_domain_reached",
	0x812  =>  "unsupported",
	0x813  =>  "invalid priority",
	0x814  =>  "not found",
	0x815  =>  "invalid credentials",
	0x816  =>  "invalid passphrase",
	0x818  =>  "no association",
);


#############################################################################
# soap setup

my ($nas, $sas, $rcs);

sub SOAP::Transport::HTTP::Client::get_basic_credentials {
	return $main::amt_user => $main::amt_pass;
}

sub soap_init() {
	my $proxybase = "$amt_proto://$amt_host:$amt_port";
	my $schemabase = "http://schemas.intel.com/platform/client";

	$nas = SOAP::Lite->new(
		proxy      => "$proxybase/NetworkAdministrationService",
		default_ns => "$schemabase/NetworkAdministration/2004/01");
	$sas = SOAP::Lite->new(
		proxy      => "$proxybase/SecurityAdministrationService",
		default_ns => "$schemabase/SecurityAdministration/2004/01");
	$rcs = SOAP::Lite->new(
		proxy      => "$proxybase/RemoteControlService",
		default_ns => "$schemabase/RemoteControl/2004/01");

	$nas->autotype(0);
	$sas->autotype(0);
	$rcs->autotype(0);

	$amt_version = $sas->GetCoreVersion()->paramsout;
}


#############################################################################
# functions

sub usage() {
	print STDERR <<EOF;

This utility can talk to Intel AMT managed machines.

usage: amttool <hostname> [ <command> ] [ <arg(s)> ]
commands:
   info            - print some machine info (default).
   reset           - reset machine.
   powerup         - turn on machine.
   powerdown       - turn off machine.
   powercycle      - powercycle machine.

AMT 2.5+ only:
   netinfo         - print network config.
   netconf <args>  - configure network (check manpage).

Password is passed via AMT_PASSWORD environment variable.

EOF
}

sub print_result($) {
	my $ret = shift;
	my $rc = $ret->result;
	my $msg;

	if (!defined($rc)) {
		$msg = "soap failure";
	} elsif (!defined($pt_status{$rc})) {
		$msg = sprintf("unknown pt_status code: 0x%x", $rc);
	} else {
		$msg = "pt_status: " . $pt_status{$rc};
	}
	printf "result: %s\n", $msg;
}

sub print_paramsout($) {
	my $ret = shift;
	my @paramsout = $ret->paramsout;
	print "params: " . join(", ", @paramsout) . "\n";
}

sub print_hash {
	my $hash = shift;
	my $in = shift;
	my $wi = shift;

	foreach my $item (sort keys %{$hash}) {
		if (ref($hash->{$item}) eq "HASH") {
#			printf "%*s%s\n", $in, "", $item;
			next;
		}
		printf "%*s%-*s%s\n", $in, "", $wi, $item, $hash->{$item};
	}
}

sub print_hash_ipv4 {
	my $hash = shift;
	my $in = shift;
	my $wi = shift;

	foreach my $item (sort keys %{$hash}) {
		my $addr = sprintf("%d.%d.%d.%d",
			$hash->{$item} / 256 / 256 / 256,
			$hash->{$item} / 256 / 256 % 256,
			$hash->{$item} / 256 % 256,
			$hash->{$item} % 256);
		printf "%*s%-*s%s\n", $in, "", $wi, $item, $addr;
	}
}

sub do_soap {
	my $soap = shift;
	my $name = shift;
	my @args = @_;
	my $method;

	$method = SOAP::Data->name($name)
			    ->attr( { xmlns => $soap->ns } );

	if ($amt_debug) {
		print "-- \n";
		open XML, "| xmllint --format -";
		print XML $soap->serializer->envelope(method => $method, @_);
		close XML;
		print "-- \n";
	}

	my $ret = $soap->call($method, @args);
	print_result($ret);
	return $ret;
}

sub check_amt_version {
	my $major = shift;
	my $minor = shift;

	$amt_version =~ m/^(\d+).(\d+)/;
	return if $1 > $major;
	return if $1 == $major && $2 >= $minor;
	die "version mismatch (need >= $major.$minor, have $amt_version)";
}

sub print_general_info() {
	printf "### AMT info on machine '%s' ###\n", $amt_host;

	printf "AMT version:  %s\n", $amt_version;
	
	my $hostname = $nas->GetHostName()->paramsout;
	my $domainname = $nas->GetDomainName()->paramsout;
	printf "Hostname:     %s.%s\n", $hostname, $domainname;

	my $powerstate = $rcs->GetSystemPowerState()->paramsout;
	printf "Powerstate:   %s\n", $ps [ $powerstate & 0x0f ];
}
	
sub print_remote_info() {
	my @rccaps = $rcs->GetRemoteControlCapabilities()->paramsout;
	printf "Remote Control Capabilities:\n";
	printf "    IanaOemNumber                   %x\n", $rccaps[0];
	printf "    OemDefinedCapabilities          %s%s%s%s%s\n",
		$rccaps[1] & 0x01 ? "IDER "        : "",
		$rccaps[1] & 0x02 ? "SOL "         : "",
		$rccaps[1] & 0x04 ? "BiosReflash " : "",
		$rccaps[1] & 0x08 ? "BiosSetup "   : "",
		$rccaps[1] & 0x10 ? "BiosPause "   : "";

	printf "    SpecialCommandsSupported        %s%s%s%s%s\n",
		$rccaps[2] & 0x0100 ? "PXE-boot "         : "",
		$rccaps[2] & 0x0200 ? "HD-boot "          : "",
		$rccaps[2] & 0x0400 ? "HD-boot-safemode " : "",
		$rccaps[2] & 0x0800 ? "diag-boot "        : "",
		$rccaps[2] & 0x1000 ? "cd-boot "          : "";

	printf "    SystemCapabilitiesSupported     %s%s%s%s\n",
		$rccaps[3] & 0x01 ? "powercycle " : "",
		$rccaps[3] & 0x02 ? "powerdown "  : "",
		$rccaps[3] & 0x03 ? "powerup "    : "",
		$rccaps[3] & 0x04 ? "reset "      : "";

	printf "    SystemFirmwareCapabilities      %x\n", $rccaps[4];
}

sub print_network_info() {
	my $ret;

	$ret = $nas->EnumerateInterfaces();
	my @if = $ret->paramsout;
	foreach my $if (@if) {
		printf "Network Interface %s:\n", $if;
		my $arg = SOAP::Data->name('InterfaceHandle' => $if);
		$ret = $nas->GetInterfaceSettings($arg);
		my $desc = $ret->paramsout;
		print_hash($ret->paramsout, 4, 32);
		print_hash_ipv4($ret->paramsout->{'IPv4Parameters'}, 8, 28);
	}
}

sub remote_control($$) {
	my $command = shift;
	my $special = shift;
	my @args;

	my $hostname = $nas->GetHostName()->paramsout;
	my $domainname = $nas->GetDomainName()->paramsout;
	printf "host %s.%s, %s [y/N] ? ", $hostname, $domainname, $command;
	my $reply = <>;
	if ($reply =~ m/^(y|yes)$/i) {
		printf "execute: %s\n", $command;
		push (@args, SOAP::Data->name('Command' => $rcc{$command}));
		push (@args, SOAP::Data->name('IanaOemNumber' => 343));
		if (defined($special) && defined($rccs{$special})) {
			push (@args, SOAP::Data->name('SpecialCommand' 
						      => $rccs{$special} ));
		}
		if (defined($special) && defined($rccs_oem{$special})) {
			push (@args, SOAP::Data->name('SpecialCommand' 
						      => $rccs_oem{$special} ));
			push (@args, SOAP::Data->name('OEMparameters' => 1 ));
		}
		do_soap($rcs, "RemoteControl", @args);
	} else {
		printf "canceled\n";
	}
}

sub ipv4_addr($$) {
	my $name = shift;
	my $ipv4 = shift;

	$ipv4 =~ m/(\d+).(\d+).(\d+).(\d+)/ or die "parse ipv4 address: $ipv4";
	my $num = $1 * 256 * 256 * 256 +
		$2 * 256 * 246 +
		$3 * 256 +
		$4;
	printf STDERR "ipv4 %-24s: %-16s -> %d\n", $name, $ipv4, $num
		if $amt_debug;
	return SOAP::Data->name($name => $num);
}

sub configure_network {
	my $if = shift;
	my $link = shift;
	my $ip = shift;
	my $mask = shift;
	my $gw = shift;
	my $dns1 = shift;
	my $dns2 = shift;

	my $mode;
	my @ifdesc;
	my @ipv4;

	my $method;
	my @args;

	# build argument structs ...
	die "no interface" if !defined($if);
	die "no linkpolicy" if !defined($link);
	if (defined($ip)) {
		$mode = "SEPARATE_MAC_ADDRESS";
		die "no ip mask"     if !defined($mask);
		die "no default gw"  if !defined($gw);
		$dns1 = $gw          if !defined($dns1);
		$dns2 = "0.0.0.0"    if !defined($dns2);
		push (@ipv4, ipv4_addr("LocalAddress", $ip));
		push (@ipv4, ipv4_addr("SubnetMask", $mask));
		push (@ipv4, ipv4_addr("DefaultGatewayAddress", $gw));
		push (@ipv4, ipv4_addr("PrimaryDnsAddress", $dns1));
		push (@ipv4, ipv4_addr("SecondaryDnsAddress", $dns2));
	} else {
		$mode = "SHARED_MAC_ADDRESS";
		# no ip info -- use DHCP
	}

	push (@ifdesc, SOAP::Data->name("InterfaceMode" => $mode));
	push (@ifdesc, SOAP::Data->name("LinkPolicy" => $link));
	push (@ifdesc, SOAP::Data->name("IPv4Parameters" => 
		\SOAP::Data->value(@ipv4)))
			if @ipv4;

	push (@args, SOAP::Data->name("InterfaceHandle" => $if));
	push (@args, SOAP::Data->name("InterfaceDescriptor" =>
		\SOAP::Data->value(@ifdesc)));

	# perform call
	do_soap($nas, "SetInterfaceSettings", @args);
}


#############################################################################
# main

if (!defined($amt_host)) {
	usage();
	exit 1;
}

soap_init;

if ($amt_command eq "info") {
	print_general_info;
	print_remote_info;
} elsif ($amt_command eq "netinfo") {
	check_amt_version(2,5);
	print_network_info;
} elsif ($amt_command eq "netconf") {
	check_amt_version(2,5);
	configure_network(@ARGV);
} elsif ($amt_command =~ m/^(reset|powerup|powerdown|powercycle)$/) {
	remote_control($amt_command, $amt_arg);
} else {
	print "unknown command: $amt_command\n";
}