This article uses the fsockopen() function to write a port scanner with simple functions. The port number in this example is fixed. By traversing the array, the fsockopen() function is used to connect. If the connection is successful, the port is open, otherwise the port is closed.
The core code is as follows:
foreach ($port as $key => $value) { echo '<tr>'; echo '<td>' . $key . '</td>'; echo '<td>' . $value . '</td>'; echo '<td>' . $msg[$key] . '</td>'; //$errno 和 $errstr 在这里基本用不上,只是为了设置 timeout,防止请求超时 $fp = @fsockopen($ip, $value, $errno, $errstr, 1);//如果主机(hostname)不可访问,将会抛出一个警告级别(E_WARNING)的错误提示。所有需要加@ $result = $fp ? '<span >开启</span>' : '<span >关闭</span>'; echo '<td>' . $result . '</td>'; echo '</tr>'; }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>端口扫描</title> <style type="text/css"> td{ padding:10px; border-bottom:1px solid #eee; } </style> </head> <body> <form method="post" action='#'> 网址/ip:<input type="text" name="ip" value="<?php echo $_POST['ip'] ?? '127.0.0.1'?>"> <button>扫描</button> </form> <table> <thead> <tr> <td>id</td> <td>端口号</td> <td>服务</td> <td>开启状态</td> </tr> </thead> <tbody> <?php $ip = $_POST['ip'] ?? '127.0.0.1'; if(ip2long($ip)){ $aIp = explode('.', $ip);//ip4地址使用.分隔符 //这里没有对 0.0.0.0 这种本机地址进行判断,只是粗略的判断ip是否合法 foreach ($aIp as $key => $value) { if($value < 0 || $value > 255){ die('地址不合法'); } } } $port = array( 21, 23, 25, 79, 80, 110, 135, 137, 138, 139, 143, 443, 445, 1433, 3306, ); $msg = array( 'Ftp', 'Telnet', 'Smtp', 'Finger', 'Http', 'Pop3', 'Location Service', 'Netbios-NS', 'Netbios-DGM', 'Netbios-SSN', 'IMAP', 'Https', 'Microsoft-DS', 'MSSQL', 'MYSQL', 'Terminal Services' ); //无论使用prot还是msg循环都是可以的,因为$key是对应的,都是索引数组 foreach ($port as $key => $value) { echo '<tr>'; echo '<td>' . $key . '</td>'; echo '<td>' . $value . '</td>'; echo '<td>' . $msg[$key] . '</td>'; //$errno 和 $errstr 在这里基本用不上,只是为了设置 timeout,防止请求超时 $fp = @fsockopen($ip, $value, $errno, $errstr, 1);//如果主机(hostname)不可访问,将会抛出一个警告级别(E_WARNING)的错误提示。所有需要加@ $result = $fp ? '<span >开启</span>' : '<span >关闭</span>'; echo '<td>' . $result . '</td>'; echo '</tr>'; } ?> </tbody> </table> </body> </html>
</html>
因为偷懒,把页面和结果都写在一起了,布局就将就把。
Introduction to main functions
fsockopen
Creates a connection based on a host name, returns a resource object successfully, returns false on failure; if the host is unavailable, a warning is thrown
Related recommendations:
lnmp enables fsockopen() function
php fsockopen() function disabling solution
The above is the detailed content of Detailed explanation of php's fsockopen() open port scanner. For more information, please follow other related articles on the PHP Chinese website!