05-24-2018, 01:14 AM
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.
Full Code:
- 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. - Let's Add the Tagline of your Logs File(portlist.ini or whatever):
PHP Code: (Select All)fputs($datei, "[Ports]\r\n");
- 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 - 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
- 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";
- Let's Write the results to your INI Logs file:
PHP Code: (Select All)fputs($datei, $print);
- Let's Close the Socket Connection to avoid Connection issues during the loop process:
PHP Code: (Select All)@fclose($handle);
- Let's Close the Loop Statement's Bracket: PHP Code: (Select All)
}
- 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);