+ Post New Thread
Results 1 to 1 of 1
New Project Ideas Thread, Web Page to turn your PC on/off via AMT/vPro in EduGeek Projects; Idea for a simple webpage that would allow users to cold power on / power off their machine remotely via ...
  1. #1
    somabc's Avatar
    Join Date
    Oct 2007
    Location
    London
    Posts
    2,337
    Thank Post
    83
    Thanked 388 Times in 258 Posts
    Rep Power
    111

    Web Page to turn your PC on/off via AMT/vPro

    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 <user> <password>

    Code:
    #!/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 = <<PON_STR;
    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 = <Reader>;
    
    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.
    Attached Files Attached Files

SHARE:
+ Post New Thread

Similar Threads

  1. web page for restoreing work
    By ful56_uk in forum Web Development
    Replies: 16
    Last Post: 6th February 2009, 04:36 PM
  2. [MS Office - XP] Web Page Wizard
    By CarlaB in forum Office Software
    Replies: 0
    Last Post: 6th November 2008, 01:46 PM
  3. Intel VPro AMT Software
    By techie08 in forum Hardware
    Replies: 2
    Last Post: 16th September 2008, 09:17 PM
  4. web page
    By tabby1039 in forum General Chat
    Replies: 6
    Last Post: 19th May 2008, 10:47 AM
  5. problem with web page in FF...
    By beeswax in forum Web Development
    Replies: 2
    Last Post: 10th March 2008, 12:31 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •