Post4VPS Forum | Free VPS Provider

Full Version: How to Check for TCP Listening Ports's Status ? (PHP)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:
    $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:
    fputs($datei"[Ports]\r\n"); 
  3. Lets initializate a Loop Statement which will be used on switching between your specified Ports:
    PHP Code:
    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:
    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:
    $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:
    fputs($datei$print); 
  7. Let's Close the Socket Connection to avoid Connection issues during the loop process:
    PHP Code:
    @fclose($handle); 
  8. Let's Close the Loop Statement's Bracket:
    PHP Code:

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

Full Code:
PHP Code:
$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); 
(05-24-2018, 01:14 AM)Zorono Wrote: [ -> ]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:
           $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):
    fputs($datei, "[Ports]\r\n");
  3. Lets initializate a Loop Statement which will be used on switching between your specified Ports:
    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':
    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:
    $print = "Port".$ports[$a]."=" . ($handle = ($host, $ports[$a], $errno, $errstr, $timeout) ? 'OPENED' : 'CLOSED') . "\r\n";
  6. Let's Write the results to your INI Logs file:
    fputs($datei, $print);
  7. Let's Close the Socket Connection to avoid Connection issues during the loop process:
    ($handle);
  8. Let's Close the Loop Statement's Bracket:
    }
  9. Let's Close your INI Logs File's Process:
    fclose($datei);

Full Code:
PHP Code:
       $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 : i really need this to check all active /inactive tcp connections. let me check it and then i gives you feedback.