Reputation: 9373
I have written a simple php page that sends wake-on-lan packets too a couple of my computers which are all running Windows 7.
How can I check if the computers are powered on (assuming the internet works at the location)?
Can I some how ping the computer when the page is loaded to check for a response?
Additional info: The computers are all at separate locations where they have dynamic dns setup for the IP and are all behind a router with other computers.
Upvotes: 2
Views: 6480
Reputation: 18859
I can remote desktop all computers via windows RDC, I opened port 3389 for them as well as a seperate WOL port so they have two open ports
Then try opening a remote desktop session?
<?php
$timeout = 10;
$socket = @fsockopen( 'ip-of-the-client', 3389, $errno, $errstr, $timeout );
$online = ( $socket !== false );
var_dump( $online );
The @
in the code is because fsockopen will throw a PHP Warning when a connection could not be made.
EDIT: for the sake of clarity; 3389 is the default port used to create a remote desktop connection.
Upvotes: 5
Reputation: 21989
Regarding this:
I can remote desktop all computers via windows RDC, I opened port 3389 for them as well as a seperate WOL port so they have two open ports.
Your best bet (other than an ICMP ping which could be blocked) since RDP is open would probably be to open a TCP socket to port 3389, which would confirm that the computer is online and fully booted (ie. the RDP service is running).
Simply do that with fsockopen, like so:
function ServiceOnline($ip, $port = 3389) {
$rdp_sock = @fsockopen($ip, $port, null, null, 5); // 5 sec timeout so that it doesn't hang for a whole minute if not available
if (!$rdp_sock) {
return false;
} else {
fclose($rdp_sock);
return true;
}
}
You could also use this code if another service is available as a fallback, for example, by passing in the $port
argument.
Upvotes: 0