Table of Contents
Use the fsockopen() function to open the port scanner
Home Backend Development PHP Tutorial Open port scanner for fsockopen() function

Open port scanner for fsockopen() function

Aug 20, 2017 am 09:12 AM
fsockopen scanner port

Use the fsockopen() function to open the port scanner


1. Foreword

This article uses the fsockopen() function to write a port scan with a simple function device.

2. Key Technology

The port number of 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 style="color:red">开启</span>' : '<span style="color:red">关闭</span>';
    echo '<td>' . $result . '</td>';
    echo '</tr>';
}
Copy after login

3. The code is as follows


<!DOCTYPE html>
<html>
<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 style="color:red">开启</span>' : '<span style="color:red">关闭</span>';
                    echo '<td>' . $result . '</td>';
                    echo '</tr>';
                }

            ?>
        </tbody>
    </table>
</body>
</html>
Copy after login
因为偷懒,把页面和结果都写在一起了,布局就将就把。
Copy after login

4. Introduction to main functions

4.1. fsockopen

Creates a connection based on a host name and returns a resource object successfully, otherwise it fails Returns false; if the host is unavailable, a warning is thrown

The above is the detailed content of Open port scanner for fsockopen() function. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Soundbar detected as monitor screen on Windows PC [Fix] Soundbar detected as monitor screen on Windows PC [Fix] Feb 19, 2024 pm 11:42 PM

Soundbar detected as monitor screen on Windows PC [Fix]

What ports are com1 and com2? What ports are com1 and com2? Aug 25, 2022 am 10:53 AM

What ports are com1 and com2?

How to check whether the remote server port is open in Linux How to check whether the remote server port is open in Linux Mar 21, 2023 am 09:50 AM

How to check whether the remote server port is open in Linux

How to close port 445 in Win11 How to close port 445 in Win11 Jul 04, 2023 pm 12:17 PM

How to close port 445 in Win11

How to close a port How to close a port Jan 17, 2024 pm 02:07 PM

How to close a port

Your iPhone won't charge after iOS 17 update? Here's what you can do Your iPhone won't charge after iOS 17 update? Here's what you can do Sep 21, 2023 pm 11:41 PM

Your iPhone won't charge after iOS 17 update? Here's what you can do

Fix AHCI Port 0 Device Error on Windows Computer Fix AHCI Port 0 Device Error on Windows Computer Feb 19, 2024 pm 12:45 PM

Fix AHCI Port 0 Device Error on Windows Computer

What is the difference between access and trunk ports? What is the difference between access and trunk ports? Oct 31, 2023 pm 05:59 PM

What is the difference between access and trunk ports?

See all articles