arrow_upward

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Check for TCP Listening Ports's Status ? (PHP)
#1
Hello everybody, so iam going to show you Today how to Check for TCP Listening Ports's Validation and Status using a small PHP script.

  1. We have to initializate some required vars:
    PHP Code: (Select All)
    $host "127.0.0.1"// The Device's remote host which will be used on Monitoring the Specified Ports.
        
    $timeout 2// The connection timeout. (in Seconds)
        
    $ports = array(80,21); // The specified Ports which will be Monitored.
        
    $datei fopen('portlist.ini''w+'); // Creating/Overwritting a file which will be used to store in the Data. 
  2. Let's Add the Tagline of your Logs File(portlist.ini or whatever):
    PHP Code: (Select All)
    fputs($datei"[Ports]\r\n"); 
  3. Lets initializate a Loop Statement which will be used on switching between your specified Ports:
    PHP Code: (Select All)
    for($i 1$i <= count($ports); $i++)
    {
            
    $a $i 1// it subtract a number ever time. and assign the result to the var $a 
  4. Let's Set the Variable's Type to Integer to avoid some stupid warnings/errors which will be caused of 'int to str':
    PHP Code: (Select All)
    settype($ports[$a], "int"); // for more info about possible Types feel free to check http://php.net/manual/en/function.settype.php 
  5. Let's do the Magic:
    PHP Code: (Select All)
    $print "Port".$ports[$a]."=" . ($handle = @fsockopen($host$ports[$a], $errno$errstr$timeout) ? 'OPENED' 'CLOSED') . "\r\n"
  6. Let's Write the results to your INI Logs file:
    PHP Code: (Select All)
    fputs($datei$print); 
  7. Let's Close the Socket Connection to avoid Connection issues during the loop process:
    PHP Code: (Select All)
    @fclose($handle); 
  8. Let's Close the Loop Statement's Bracket:
    PHP Code: (Select All)

  9. Let's Close your INI Logs File's Process:
    PHP Code: (Select All)
    fclose($datei); 

Full Code:
PHP Code: (Select All)
$host "127.0.0.1";
    
$timeout 2;
    
    
$ports = array(80,21);
    
$datei fopen('portlist.ini''w+');
    
fputs($datei"[Ports]\r\n");
    for(
$i 1$i <= count($ports); $i++)
    {
        
$a $i 1;
        
settype($ports[$a], "int");
        
$print "Port".$ports[$a]."=" . ($handle = @fsockopen($host$ports[$a], $errno$errstr$timeout) ? 'OPENED' :  'CLOSED') . "\r\n";
        
fputs($datei$print);
        @
fclose($handle);
    }
    
fclose($datei); 
Thanks to @Post4VPS & @Hostdare for Providing their VPS Hosting Services: VPS 12


Messages In This Thread
How to Check for TCP Listening Ports's Status ? (PHP) - by Zorono - 05-24-2018, 01:14 AM

Possibly Related Threads…
Thread
Author
Replies
Views
Last Post

person_pin_circle Users browsing this thread: 2 Guest(s)
Sponsors: VirMach - Host4Fun - CubeData - Evolution-Host - HostDare - Hyper Expert - Shadow Hosting - Bladenode - Hostlease - RackNerd - ReadyDedis - Limitless Hosting