ASPit - Totally ASP

Go Back PHPit Forums > General PHP > Just Starting
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
Thread Tools Search this Thread Display Modes
Old 11-25-2005, 06:06 PM #1
Leigh
Junior Member

Join Date: Nov 2005
Location: Ontario, Canada
Posts: 10
Default How to determine if a PC is on line?

I'm writing an inventory program that goes out and interrogates a PC for its hardware and software. However, before I do this I want to ensure that the PC is switched on and on line. How can I do this in PHP? I don't really want to shell out and PING the computer, I'm just hoping there's a more subtle method.

The network is all Windows, with XP Pro desktops/laptops and NT4/WK2003 servers.
Leigh is offline Reply With Quote
Old 11-27-2005, 07:42 PM #2
Dennis Pallett
Administrator

Dennis Pallett's Avatar

Join Date: Nov 2005
Posts: 258
Default

Maybe you can try something with fopen or file_exists? For example:

PHP Code:
if(file_exists('/path/to/file/on/the/other/computer')==true){
//computerisonline
else{
//computerisoffline(orfiledoesn'texist,somakesurethefiledoesexist)
}

Something like that maybe?

I've never had to something like this to be honest, and I don't know of any other practical way (short of using shell and ping like you mentioned).
Dennis Pallett is offline Reply With Quote
Old 11-28-2005, 08:47 PM #3
Leigh
Junior Member

Join Date: Nov 2005
Location: Ontario, Canada
Posts: 10
Default

Well, after much digging around I discovered the fsockopen function on PHP.NET and cobbled together this little snippet of code:

PHP Code:
$isThere=@fsockopen("10.0.0.25",135,$errno,$errstr,10);
if(!
$isThere){
echo
"Offline";
}else{
echo
"Online";
}
It's not a true PING as PING doesn't really use a port, but fsockopen requires a port to be specified. I used the infamous TCP port 135. The other parameters aren't required for this little snippet, but I left them there for completeness.

The other problem I had with this snippet was that if a PC is offline, I got a couple of PHP warnings that broke the display. Once again, Google to the rescue, I found that putting the "@" sign in front of the function suppressed the warnings. That was new one to me!
Leigh is offline Reply With Quote
Old 11-29-2005, 04:39 PM #4
Dennis Pallett
Administrator

Dennis Pallett's Avatar

Join Date: Nov 2005
Posts: 258
Default

Excellent way of doing it, the only downside is the need for 135 which is often blocked. But if it's only for internal use, it should be fine!

I'm going to wrap this up in a FAQ on PHPit.
Dennis Pallett is offline Reply With Quote
Old 11-30-2005, 06:45 PM #5
Leigh
Junior Member

Join Date: Nov 2005
Location: Ontario, Canada
Posts: 10
Default

Indeed, you are right, Dennis, port 135 may be blocked for security reasons. I am using this internally so the security risk isn't that great, and I also had to choose a port that I could guarantee to be available on our Windows workstations and servers. For anyone who's interested in finding other ports that may be used try this from a command line:

netstat -an |find /i "listening"
Leigh is offline Reply With Quote
Old 12-02-2005, 09:06 PM #6
Dennis Pallett
Administrator

Dennis Pallett's Avatar

Join Date: Nov 2005
Posts: 258
Default

I've posted this as a FAQ, available at http://phpit.net/faq/determining-if-a-pc-is-online/. Thanks for the tip!
Dennis Pallett is offline Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT. The time now is 04:21 PM.


Powered by vBulletin Version 3.6.0
Copyright 2000 - 2007, Jelsoft Enterprises Ltd.