扫描局域网内80端口


package main

import (
	"fmt"
	"net"
	"strconv"
)

func main() {

	count := 254
	run := make([]chan int, count)

	for index := 0; index < count; index++ {
		run[index] = make(chan int)
	}

	for index := 0; index < count; index++ {
		go func(i int) {
			ip := "192.168.1." + strconv.Itoa(i+1)
			p := scanPort(ip)
			fmt.Println(ip, "\t", p)
			run[i] <- i
		}(index)
	}

	for index := 0; index < count; index++ {
		<-run[index]
	}

}

func scanPort(ip string) bool {
	conn, err := net.Dial("tcp", ip+":80")
	if err != nil {
		return false
	}
	defer conn.Close()
	return true
}