#!/usr/bin/perl use strict; use warnings; use SOAP::Lite; #use SOAP::Lite +trace => 'all'; my $amt_host = shift; my $amt_port = "16992"; $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_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", ); # 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 = "http://$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 < [ ] 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 - 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 @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 ($command ne 'powerdown') { push (@args, SOAP::Data->name('OEMparameters' => 1 )); push (@args, SOAP::Data->name('SpecialCommand' => 0xC1 )); push (@args, SOAP::Data->name('SpecialCommandParameter' => 0)); } 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); } else { print "unknown command: $amt_command\n"; }