Jump to content

Recommended Posts

Posted

Idea for a simple webpage that would allow users to cold power on / power off their machine remotely via Intel vPro.

 

Could utilise Perl Script

amtpower.pl on|off|status IP

 

#!/usr/bin/perl
# Machine power state control using AMT engine
# v1.00

use warnings;
use strict;

use FileHandle;
use IPC::Open2;

use constant DEBUG => 0; #  assigning a non-zero value here will turn on debug printouts

# TODO - provide all necessary binaries OR somehow automate the path
my $bin_dir = '';
my $remote_control = $bin_dir . 'RemoteControl.exe';

sub print_usage_and_die {
print "USAGE: amtpower [on|off|status] IPaddress [user password]\n";
die shift;
}

# parsing command line parameters
my $op = shift @ARGV;
print_usage_and_die "Insufficient number of arguments.\n" unless ($op);

my $pwr_action = 0;
my $pwr_state_only = 0;
if ($op =~ /on|off|status|state/i) {
$pwr_action = ($op =~ /on/i);
$pwr_state_only = ($op =~ /status|state/i);
$op = shift @ARGV;
print_usage_and_die "Insufficient number of arguments.\n" unless ($op);
} else {
$pwr_state_only = 1;
}

my $ip_address = $op;
unless ($op =~
/^([1-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-4])(\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-4])){2}\.([1-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-4])$/
	) {
print_usage_and_die "Command line parameter \"$op\" does not look like a valid IP address.\n";
}

my $user = 'admin';
my $pass = 'P@ssw0rd';

$op = shift @ARGV;
if ($op) {
$user = $op;
$pass = shift @ARGV;
print_usage_and_die "Insufficient number of arguments.\n" unless ($pass);
}

# other "constants" definitions
my $power_on_txt = <17
343
-1
-1
-1
-1
PON_STR

my $power_off_txt = q~
18
343
~;

my $pon_cmd = "$remote_control -r -user $user -pass $pass http://$ip_address:16992/RemoteControlService";

my %pwr_s_states = ( -1 => 'Unknown', 0 => 'On', 1 => 'Sleep', 2 => 'Sleep', 3 => 'Sleep', 4 => 'Hibernate', 5 => 'Off' );

# start with checking the current power state of a system
# TODO: implement a quick verification of AMT engine presence at a given address before calling the tool [will be implemented for the site scan as well]

# no user input needed for this command - let's use backtick trick to call the tool
my @results = `$remote_control -p -user $user -pass $pass http://$ip_address:16992/RemoteControlService`;
print "> The return code of RemoteControl.exe -p was: $?\n" if (DEBUG());

my $initial_pwr_state = -1;
for (@results) {
print ":$_" if (DEBUG());
chomp;
if (/is: (\d*)/) {
	print '> initial power state was = ' . $1 . "\n" if (DEBUG());
	$initial_pwr_state = int $1;
} elsif (/failed|failure|error/i) {
	die "Failed to access Management Engine.\nPlease check the IP address and login credentials and try again.\n";
}
}

# if asked - report the obtained power state and exit
if ($pwr_state_only) {
my $sname = $pwr_s_states{ $initial_pwr_state };
print "Reported system power state is $sname (S$initial_pwr_state).\n";
exit;
}

# check for potential mis-behaivings; should we treat this as an error?
if (0 > $initial_pwr_state || 5 < $initial_pwr_state) {
print "Reported system power state was $initial_pwr_state. Handling of this situation is not implemented yet. Doing nothing.\n";
exit;
}

# do we actually need to change the power state?
if ((!$pwr_action && 0 < $initial_pwr_state) || ($pwr_action && 0 == $initial_pwr_state)) {
print 'The system reported power ' . ($pwr_action?"on":"off") . " (S$initial_pwr_state) state already. Doing nothing.\n";
exit;
}

# flip the power switch
my $pid = open2( *Reader, *Writer, $pon_cmd );

print Writer $pwr_action?$power_on_txt:$power_off_txt;
@results = ;

for (@results) {
print ":$_" if (DEBUG());
chomp;
if (/RemoteControl...\s*success/) {
	print "> OK\n" if (DEBUG());
}
}

# TODO: what would be the script return code agreement?
# Currently script calls "die" (http://perldoc.perl.org/functions/die.html) on errors or exits gracefully on success or when there's nothing to be done.

amt.zip

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...