arrow_upward

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Check for TCP Listening Ports's Status ? (PHP)
#2
(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 = @fsockopen($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:
    @fclose($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: (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 @Zorono : i really need this to check all active /inactive tcp connections. let me check it and then i gives you feedback.
Heart LOVE FOR ALL  HATRED FOR NONE Heart


Messages In This Thread
RE: How to Check for TCP Listening Ports's Status ? (PHP) - by sagher - 05-26-2018, 03:59 AM

Possibly Related Threads…
Thread
Author
Replies
Views
Last Post

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