Home > Article > Backend Development > Detailed explanation of php's fsockopen() open port scanner
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 ''; echo ' '; }' . $key . ' '; echo '' . $value . ' '; echo '' . $msg[$key] . ' '; //$errno 和 $errstr 在这里基本用不上,只是为了设置 timeout,防止请求超时 $fp = @fsockopen($ip, $value, $errno, $errstr, 1);//如果主机(hostname)不可访问,将会抛出一个警告级别(E_WARNING)的错误提示。所有需要加@ $result = $fp ? '开启' : '关闭'; echo '' . $result . ' '; echo '
端口扫描
id | 端口号 | 服务 | 开启状态 |
' . $key . ' | '; echo '' . $value . ' | '; echo '' . $msg[$key] . ' | '; //$errno 和 $errstr 在这里基本用不上,只是为了设置 timeout,防止请求超时 $fp = @fsockopen($ip, $value, $errno, $errstr, 1);//如果主机(hostname)不可访问,将会抛出一个警告级别(E_WARNING)的错误提示。所有需要加@ $result = $fp ? '开启' : '关闭'; echo '' . $result . ' | '; echo '
因为偷懒,把页面和结果都写在一起了,布局就将就把。
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!