Search code examples
phpstatus

Status checker for hundreds IP addresses


I wonder how to make a status checker, checking about 500 addresses in a few minutes? (it'll check a certain port if its listening).

But I care about the performance... so I don't know if it can be done with PHP. Waiting for your suggestions guys.

Also please give me some examples, if you think that the best way for this thing will be PHP or C#.

Ofc. I meant the TCP connection but not http, since I need to check open port for example: 11740

Edit:

Added third bounty for this question! Please post some better answer than those already posted.


Solution

  • The best way to this would be nmap, as mentioned in other answers.

    You'd want to run it like this (-PN is don't ping, -sP means to skip the port scan and just check if the host is up, -PS80 means to check port 80, -n is not to do reverse DNS lookup, -oG - is to output in machine readable format, and the other arguments are IP addresses or hostnames):

    nmap -PN -sP -PS80 -n -oG - --send-ip IP1 IP2 ...
    

    And it would look like this:

    $ nmap -n -PN -sP -PS80 -oG -  209.85.147.104 87.248.122.122 4.4.4.4
    # Nmap 5.21 scan initiated Tue Feb 21 01:07:20 2012 as: nmap -n -PN -sP -PS80 -oG - 209.85.147.104 87.248.122.122 4.4.4.4 
    Host: 209.85.147.104 () Status: Up
    Host: 87.248.122.122 () Status: Up
    Host: 4.4.4.4 ()    Status: Down
    # Nmap done at Tue Feb 21 01:07:21 2012 -- 3 IP addresses (2 hosts up) scanned in 0.95 seconds
    

    You could run and parse this from PHP with no trouble. I'm not very experienced in PHP, and haven't tested this, but here's some example code:

    <?php
    $output = shell_exec('nmap -n -PN -sP -PS80 -oG - --send-ip ' . implode(" ", $ips));
    $result = array();
    foreach(preg_split("/\r?\n/", $output) as $line) {
        if (!(substr($line, 0, 1) === "#")) {
            $info = preg_split("[\t ]", $line);
            $result[$info[2]] = ($info[5] === "Up");
        }
    }
    ?>
    

    Mind you, writing PHP code or C# code or whatever that does this isn't a big deal, it's just that nmap is very very good at what it does, and extremely versatile, that writing code that does this is reinventing the wheel. If you do decide to go that route, make sure you make your code asynchronous, otherwise one slow server would slow down your entire sweep.