0

0

一文详解Go如何实现端口扫描器

藏色散人

藏色散人

发布时间:2023-04-14 16:53:31

|

2054人浏览过

|

来源于learnku

转载

本篇文章给大家带来了关于go的相关知识,其中主要跟大家介绍go怎么实现端口扫描器,有代码示例,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。

DeepAI
DeepAI

为天生具有创造力的人提供的AI工具

下载
利用 GO 批量扫描服务器端口

1、端口扫描器 V1 - 基本操作

package mainimport (
    "fmt"
    "net"
    "time"
    "unsafe")func main() {
    tcpScan("127.0.0.1", 1, 65535)}func tcpScan(ip string, portStart int, portEnd int) {
    start := time.Now()

    // 参数校验
    isok := verifyParam(ip, portStart, portEnd)
    if isok == false {
        fmt.Printf("[Exit]\n")
    }

    for i := portStart; i <= portEnd; i++ {
        address := fmt.Sprintf("%s:%d", ip, i)
        conn, err := net.Dial("tcp", address)
        if err != nil {
            fmt.Printf("[info] %s Close \n", address)
            continue
        }
        conn.Close()
        fmt.Printf("[info] %s Open \n", address)
    }

    cost := time.Since(start)
    fmt.Printf("[tcpScan] cost %s second \n", cost)}func verifyParam(ip string, portStart int, portEnd int) bool {
    netip := net.ParseIP(ip)
    if netip == nil {
        fmt.Println("[Error] ip type is must net.ip")
        return false
    }
    fmt.Printf("[Info] ip=%s | ip type is: %T | ip size is: %d \n", netip, netip, unsafe.Sizeof(netip))

    if portStart < 1 || portEnd > 65535 {
        fmt.Println("[Error] port is must in the range of 1~65535")
        return false
    }
    fmt.Printf("[Info] port start:%d end:%d \n", portStart, portEnd)

    return true}

2、端口扫描器 V2 - 使用 goroutine

package mainimport (
    "fmt"
    "net"
    "sync"
    "time"
    "unsafe")func main() {
    tcpScanByGoroutine("127.0.0.1", 1, 65535)}func tcpScanByGoroutine(ip string, portStart int, portEnd int) {
    start := time.Now()
    // 参数校验
    isok := verifyParam(ip, portStart, portEnd)
    if isok == false {
        fmt.Printf("[Exit]\n")
    }

    var wg sync.WaitGroup    for i := portStart; i <= portEnd; i++ {
        wg.Add(1)

        go func(j int) {
            defer wg.Done()
            address := fmt.Sprintf("%s:%d", ip, j)
            conn, err := net.Dial("tcp", address)
            if err != nil {
                fmt.Printf("[info] %s Close \n", address)
                return
            }
            conn.Close()
            fmt.Printf("[info] %s Open \n", address)
        }(i)
    }
    wg.Wait()

    cost := time.Since(start)
    fmt.Printf("[tcpScanByGoroutine] cost %s second \n", cost)}func verifyParam(ip string, portStart int, portEnd int) bool {
    netip := net.ParseIP(ip)
    if netip == nil {
        fmt.Println("[Error] ip type is must net.ip")
        return false
    }
    fmt.Printf("[Info] ip=%s | ip type is: %T | ip size is: %d \n", netip, netip, unsafe.Sizeof(netip))

    if portStart < 1 || portEnd > 65535 {
        fmt.Println("[Error] port is must in the range of 1~65535")
        return false
    }
    fmt.Printf("[Info] port start:%d end:%d \n", portStart, portEnd)

    return true}

3、端口扫描器 V3 - 利用 Goroutine + Channel

package mainimport (
    "fmt"
    "net"
    "sync"
    "time"
    "unsafe")func main() {
    tcpScanByGoroutineWithChannel("127.0.0.1", 1, 65535)}func handleWorker(ip string, ports chan int, wg *sync.WaitGroup) {
    for p := range ports {
        address := fmt.Sprintf("%s:%d", ip, p)
        conn, err := net.Dial("tcp", address)
        if err != nil {
            fmt.Printf("[info] %s Close \n", address)
            wg.Done()
            continue
        }
        conn.Close()
        fmt.Printf("[info] %s Open \n", address)
        wg.Done()
    }}func tcpScanByGoroutineWithChannel(ip string, portStart int, portEnd int) {
    start := time.Now()

    // 参数校验
    isok := verifyParam(ip, portStart, portEnd)
    if isok == false {
        fmt.Printf("[Exit]\n")
    }

    ports := make(chan int, 100)
    var wg sync.WaitGroup    for i := 0; i < cap(ports); i++ {
        go handleWorker(ip, ports, &wg)
    }

    for i := portStart; i <= portEnd; i++ {
        wg.Add(1)
        ports <- i    }

    wg.Wait()
    close(ports)

    cost := time.Since(start)
    fmt.Printf("[tcpScanByGoroutineWithChannel] cost %s second \n", cost)}func verifyParam(ip string, portStart int, portEnd int) bool {
    netip := net.ParseIP(ip)
    if netip == nil {
        fmt.Println("[Error] ip type is must net.ip")
        return false
    }
    fmt.Printf("[Info] ip=%s | ip type is: %T | ip size is: %d \n", netip, netip, unsafe.Sizeof(netip))

    if portStart < 1 || portEnd > 65535 {
        fmt.Println("[Error] port is must in the range of 1~65535")
        return false
    }
    fmt.Printf("[Info] port start:%d end:%d \n", portStart, portEnd)

    return true}

4 端口扫描器 V4 - 引入两个 Channel

// packagepackage mainimport (
    "fmt"
    "net"
    "sort"
    "time"
    "unsafe")func main() {
    tcpScanByGoroutineWithChannelAndSort("127.0.0.1", 1, 65535)}// The function handles checking if ports are open or closed for a given IP address.func handleWorker(ip string, ports chan int, results chan int) {
    for p := range ports {
        address := fmt.Sprintf("%s:%d", ip, p)
        conn, err := net.Dial("tcp", address)
        if err != nil {
            // fmt.Printf("[debug] ip %s Close \n", address)
            results <- (-p)
            continue
        }
        // fmt.Printf("[debug] ip %s Open \n", address)
        conn.Close()
        results <- p    }}func tcpScanByGoroutineWithChannelAndSort(ip string, portStart int, portEnd int) {
    start := time.Now()

    // 参数校验
    isok := verifyParam(ip, portStart, portEnd)
    if isok == false {
        fmt.Printf("[Exit]\n")
    }

    ports := make(chan int, 50)
    results := make(chan int)
    var openSlice []int
    var closeSlice []int

    // 任务生产者-分发任务 (新起一个 goroutinue ,进行分发数据)
    go func(a int, b int) {
        for i := a; i <= b; i++ {
            ports <- i        }
    }(portStart, portEnd)

    // 任务消费者-处理任务  (每一个端口号都分配一个 goroutinue ,进行扫描)
    // 结果生产者-每次得到结果 再写入 结果 chan 中
    for i := 0; i < cap(ports); i++ {
        go handleWorker(ip, ports, results)
    }

    // 结果消费者-等待收集结果 (main中的 goroutinue 不断从 chan 中阻塞式读取数据)
    for i := portStart; i <= portEnd; i++ {
        resPort := <-results        if resPort > 0 {
            openSlice = append(openSlice, resPort)
        } else {
            closeSlice = append(closeSlice, -resPort)
        }
    }

    // 关闭 chan
    close(ports)
    close(results)

    // 排序
    sort.Ints(openSlice)
    sort.Ints(closeSlice)

    // 输出
    for _, p := range openSlice {
        fmt.Printf("[info] %s:%-8d Open\n", ip, p)
    }
    // for _, p := range closeSlice {
    //     fmt.Printf("[info] %s:%-8d Close\n", ip, p)
    // }

    cost := time.Since(start)
    fmt.Printf("[tcpScanByGoroutineWithChannelAndSort] cost %s second \n", cost)}func verifyParam(ip string, portStart int, portEnd int) bool {
    netip := net.ParseIP(ip)
    if netip == nil {
        fmt.Println("[Error] ip type is must net.ip")
        return false
    }
    fmt.Printf("[Info] ip=%s | ip type is: %T | ip size is: %d \n", netip, netip, unsafe.Sizeof(netip))

    if portStart < 1 || portEnd > 65535 {
        fmt.Println("[Error] port is must in the range of 1~65535")
        return false
    }
    fmt.Printf("[Info] port start:%d end:%d \n", portStart, portEnd)

    return true}

相关专题

更多
Golang channel原理
Golang channel原理

本专题整合了Golang channel通信相关介绍,阅读专题下面的文章了解更多详细内容。

243

2025.11.14

golang channel相关教程
golang channel相关教程

本专题整合了golang处理channel相关教程,阅读专题下面的文章了解更多详细内容。

342

2025.11.17

java学习网站推荐汇总
java学习网站推荐汇总

本专题整合了java学习网站相关内容,阅读专题下面的文章了解更多详细内容。

3

2026.01.08

java学习网站汇总
java学习网站汇总

本专题整合了java学习网站相关内容,阅读专题下面的文章了解更多详细内容。

0

2026.01.08

正则表达式 删除
正则表达式 删除

本专题整合了正则表达式删除教程大全,阅读专题下面的文章了解更多详细教程。

19

2026.01.08

java 元空间 永久代
java 元空间 永久代

本专题整合了java中元空间和永久代的区别,阅读专题下面的文章了解更多详细内容。

3

2026.01.08

java 永久代和元空间
java 永久代和元空间

本专题整合了java中元空间和永久代的区别,阅读专题下面的文章了解更多详细内容。

0

2026.01.08

java成品网站源码资源大全
java成品网站源码资源大全

本专题整合了java成品网站源码相关内容,阅读专题下面的文章了解更多详细内容。

9

2026.01.08

java过滤器教程大全
java过滤器教程大全

本专题整合了java过滤器相关教程,阅读专题下面的文章了解更多详细内容。

4

2026.01.08

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Go 教程
Go 教程

共32课时 | 3.5万人学习

Go语言实战之 GraphQL
Go语言实战之 GraphQL

共10课时 | 0.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号