PHP Checking if a port is Active

点点圈 提交于 2019-12-25 06:27:21

问题


I'm in the process of creating my own service status script as both a chance to become more familiar with the PHP language and to design it from the ground up as being as efficient as possible for my needs.

A section of my code used in both my cron job and testing a connection parts queries the IP/Port of a service to make sure it is online. My issue is that the script simply queries whether the port is "Unblocked" on that IP so if for instance I was querying port 21 with an FTP server and that FTP server crashed my script would not detect any changes meaning its not doing what I want it to do. Instead I would be wanting the IP and port to be queried and for my script to see if there is actually something running on that port, if there is show online if not error out. I've had a look on google and it seems like I would have to send a packet/receive a response so PHP can tell there's something active? I'm not sure.

This is my current code below:

<?php
    $host = $_POST['servip'];
    $port = $_POST['servport'];

    if (!$socket = @fsockopen($host, $port, $errno, $errstr, 3)) {
        echo "Offline!";
    } else {
        echo "Online!";
        fclose($socket);
    }
?>

回答1:


http://php.net/manual/en/function.fsockopen.php

fsockopen — Open Internet or Unix domain socket connection The socket will by default be opened in blocking mode. You can switch it to non-blocking mode by using stream_set_blocking(). The function stream_socket_client() is similar but provides a richer set of options, including non-blocking connection and the ability to provide a stream context.

Since fsockopen will either connect or not connect (timeout) then that tells you whether or not a connection is available ("open") or being blocked (firewall, etc).

// Ping by website domain name, IP address or Hostname
function example_pingDomain($domain){

    $starttime = microtime(true);
    $file      = @fsockopen($domain, 80, $errno, $errstr, 10);
    $stoptime  = microtime(true);
    $status    = 0;

    if (!$file) { 
        $status = -1;  // Site is down

    } else {

        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}



回答2:


If you really want to know if the FTP server is working or not, your best option is to actually send FTP commands through to it.

An FTP server, upon connect, should typically reply with the first three bytes "220" or "120". 220 is a "greeting". You can read more in RFC 959.

To be completely sure, you might be better off using ftp:// handling in PHP, e.g. actually authenticating a user (maybe user authentication is broken, but it's still able to send a greeting - does that count is "down"?)

Anyway, if you want better than "was I able to connect on that port?" or "did the connect succeed in a timely fashion?", you have to delve into actual communication over the socket. Ultimately, this means you have to do something special for each type of service (for some, read bytes, for others write bytes, etc.)



来源:https://stackoverflow.com/questions/36759307/php-checking-if-a-port-is-active

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!