} elsif (defined $amt) {
use LWP::UserAgent;
use LWP::Authen::Digest;
- sub genXML { #HOST, username, password, schema, className, pstate
+
+ sub genXML {
my ($host, $username, $password, $schema, $className, $pstate) = @_;
- #AMT numbers for PowerStateChange: 2 on, 4 standby, 7 hibernate, 8 off,
- #10 reset, 11 MNI interupt(on windows->bluescreen;-))
- my %pstates = ("on", 2,
- "standby", 4,
- "hibernate", 7,
- "off", 8,
- "reset", 10,
- "MNI", 11);
- my $text = <<END;
+ #AMT numbers for PowerStateChange (MNI => bluescreen on windows;-)
+ my %pstates = ("on" => 2,
+ "standby" => 4,
+ "hibernate" => 7,
+ "off" => 8,
+ "reset" => 10,
+ "MNI" => 11);
+ return <<END;
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd">
<s:Header><a:To>http://$host:16992/wsman</a:To>
<w:ResourceURI s:mustUnderstand="true">$schema</w:ResourceURI>
</p:RequestPowerStateChange_INPUT>
</s:Body></s:Envelope>
END
- return $text;
}
- sub sendPOST{ #ip, username, password, content
- my ($host, $username, $password, $content)=@_;+
- my $req = HTTP::Request->new(POST => "http://$host:16992/wsman");
+ sub sendPOST {
+ my ($host, $username, $password, $content) = @_;
+
my $ua = LWP::UserAgent->new();
+ $ua->agent("novaboot");
+
+ my $req = HTTP::Request->new(POST => "http://$host:16992/wsman");
my $res = $ua->request($req);
- die ($res->status_line) unless $res->code==401;
- my $header = $res->header("WWW-Authenticate");
- my $digRealm = "Digest realm=\"";
- my $posRealm = index($header,$digRealm)+length($digRealm);
- my $realm = substr($header,$posRealm,index($header,"\"",$posRealm)-$posRealm);
+ die ("Unexpected AMT response: " . $res->status_line) unless $res->code == 401;
+
+ my ($realm) = $res->header("WWW-Authenticate") =~ /Digest realm="(.*?)"/;
+ $ua->credentials("$host:16992", $realm, $username => $password);
- $ua = LWP::UserAgent->new();
- $ua->agent("novaboot");
- $ua->credentials("$host:16992",$realm, $username => $password);
# Create a request
$req = HTTP::Request->new(POST => "http://$host:16992/wsman");
$req->content_type('application/x-www-form-urlencoded');
$req->content($content);
$res = $ua->request($req);
- die ($res->status_line) unless $res->is_success;
+ die ("AMT power change request failed: " . $res->status_line) unless $res->is_success;
$res->content() =~ /<g:ReturnValue>(\d+)<\/g:ReturnValue>/;
return $1;
}
- sub powerChange {#IP, username, password, pstate
+ sub powerChange {
my ($host, $username, $password, $pstate)=@_;
my $schema="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_PowerManagementService";
my $className="/RequestPowerStateChange";
return sendPOST($host, $username, $password, $content);
}
- my (@amt_info, $len, $amt_password, $result, $host, $port, $user);
- ($user,$amt_password,$host,$port) = ($amt =~ /(?:(.*?)(?::(.*))?@)?([^:]*)(?::([0-9]*))?/);;
- $len=@amt_info;
- die "AMT host not specified" unless $host;
+ my ($user,$amt_password,$host,$port) = ($amt =~ /(?:(.*?)(?::(.*))?@)?([^:]*)(?::([0-9]*))?/);;
$user ||= "admin";
- $port ||= 16994;
$amt_password ||= $ENV{'AMT_PASSWORD'} || die "AMT password not specified";
+ $host || die "AMT host not specified";
+ $port ||= 16994;
+
$target_power_off = sub {
- $result = powerChange($host,$user,$amt_password, "off");
- die "Cannot turn off the computer, somebody is connected or computer is already off" unless $result==0;
+ my $result = powerChange($host,$user,$amt_password, "off");
+ die "AMT power off failed (ReturnValue $result)" if $result != 0;
};
$target_power_on = sub {
- $result = powerChange($host,$user,$amt_password, "on");
+ my $result = powerChange($host,$user,$amt_password, "on");
+ die "AMT power on failed (ReturnValue $result)" if $result != 0;
};
$target_reset = sub {
- $result = powerChange($host,$user,$amt_password, "reset");
+ my $result = powerChange($host,$user,$amt_password, "reset");
if ($result != 0) {
print STDERR "Warning: Cannot reset $host, trying power on. ";
$result = powerChange($host,$user,$amt_password, "on");
}
- die "AMT reset failed (code $result)" if $result;
- sleep(2); # Without pause, connection to AMT machine is initiated before reset and fails after restart
+ die "AMT reset failed (ReturnValue $result)" if $result != 0;
+
+ sleep(2); # Without pause, connection to AMT machine is
+ # initiated before reset and fails after restart
+
+ # amtterm must be run here because AMT doesn't allow to
+ # reset/on/off computer when somebody is connected
$exp = Expect->spawn("amtterm -u $user -p $amt_password $host $port");
- #expect must be here because AMT doesnt allow to reset/on/off computer when somebody is connected
};
}