05-26-2018, 03:59 AM
(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.
- 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.- Let's Add the Tagline of your Logs File(portlist.ini or whatever):
fputs($datei, "[Ports]\r\n");- 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- 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- Let's do the Magic:
$print = "Port".$ports[$a]."=" . ($handle = @fsockopen($host, $ports[$a], $errno, $errstr, $timeout) ? 'OPENED' : 'CLOSED') . "\r\n";- Let's Write the results to your INI Logs file:
fputs($datei, $print);- Let's Close the Socket Connection to avoid Connection issues during the loop process:
@fclose($handle);- Let's Close the Loop Statement's Bracket:
}- 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.
LOVE FOR ALL HATRED FOR NONE