Ullaakut / nmap

Idiomatic nmap library for go developers
MIT License
902 stars 101 forks source link

Error running scan: exit status 1 #129

Open wowlikon opened 2 months ago

wowlikon commented 2 months ago

When i run this code

package main

import (
    "context"
    "fmt"
    "net"
    "time"

    "github.com/Ullaakut/nmap/v3"
)

func tcpGather(ip string, ports []string) map[string]bool {
    results := make(map[string]bool)
    for _, port := range ports {
        address := net.JoinHostPort(ip, port)
        conn, err := net.DialTimeout("tcp", address, time.Second/2)
        if err != nil {
            results[port] = false
        } else {
            if conn != nil {
                results[port] = true
                _ = conn.Close()
            } else {
                results[port] = false
            }
        }
    }
    return results
}

func main() {
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()

    // Create a new nmap scanner
    scanner, err := nmap.NewScanner(
        ctx,
        nmap.WithTargets("192.168.0.0/24"), // Specify the network range to scan
        nmap.WithFastMode(),                // Enable fast mode
    )

    if err != nil {
        fmt.Println("Error creating scanner:", err)
        return
    }

    // Run the scan
    result, _, err := scanner.Run()

    if err != nil {
        fmt.Println("Error running scan:", err)
        return
    }

    // Print the results
    for _, host := range result.Hosts {
        if len(host.Addresses) == 0 {
            continue
        }

        ip := host.Addresses[0].Addr

        name := "Unknown"
        if len(host.Hostnames) == 1 {
            name = host.Hostnames[0].Name
        } else if len(host.Hostnames) > 1 {
            name = "["
            for _, current_name := range host.Hostnames {
                name += current_name.Name + ", "
            }
            name += "]"
        }

        mac := ""
        if len(host.Addresses) > 1 {
            mac = host.Addresses[1].Addr
        }

        fmt.Printf(
            "NAME: %s\nIP: %s\nMAC: %s\n",
            name, ip, mac,
        )

        ports := tcpGather(ip,
            []string{"20", "21", "22", "80", "135", "139", "143", "443", "445", "3020", "3306", "3389", "8022", "8080"},
        )
        if len(ports) != 0 {
            fmt.Println("PORTS:")
            for port, status := range ports {
                fmt.Printf("\t%-5s: %5t\n", string(port), status)
            }
        }
        fmt.Print("\n\n")
    }
}

result: Error running scan: exit status 1 tested on windows 10 and windows 11

Ullaakut commented 2 months ago

Hi @wowlikon ! Which version of nmap do you use? Is it available in your PATH?