func main() {
for i := 22; i < 1000; i++ {
address := fmt.Sprintf("192.168.141.135:%d", i)
connec, err := net.Dial("tcp", address)
if err != nil {
fmt.Println(i, "端口打开失败")
continue
}
defer connec.Close()
fmt.Println(i, "端口打开成功")
}
}
使用单线程问题是比较慢。扫描完大概是半个小时
多线程func main() {
var wg sync.WaitGroup
for i := 22; i < 5535; i++ {
wg.Add(1)
go func(j int) {
defer wg.Done()
address := fmt.Sprintf("192.168.141.135:%d", j)
conn, err := net.Dial("tcp", address)
if err != nil {
// fmt.Println(address, "端口关闭了")
return
}
defer conn.Close()
fmt.Println(address, "端口打开了")
}(i)
}
wg.Wait()
}
工作的结构如下
代码
- 主线程和处理的
go
应该是并发执行。
func work(port chan int, wg *sync.WaitGroup, result chan int) {
for p := range port {
defer wg.Done()
fmt.Println(p)
address := fmt.Sprintf("192.168.141.135:%d", p) //扫描地址
conn, err := net.Dial("tcp", address)
if err != nil {
result <- 0 //
continue
}
defer conn.Close() //关闭
result <- p //把端口传回去就可以了
}
}
func main() {
ports := make(chan int, 100) //带有100个缓冲池
results := make(chan int) //这个只有main自己干,他就不需要缓冲
openport, closeport := make([]int, 1), make([]int, 1)
var wg sync.WaitGroup
for i := 0; i < cap(ports); i++ {
go work(ports, &wg, results) //相当于搞了 100 个干活的。
}
go func() {
for i := 1; i < 1024; i++ {
wg.Add(1)
ports <- i
}
}()
for i := 1; i < 1024; i++ {
port := <-results //返回的数据
if port != 0 {
openport = append(openport, port)
} else {
closeport = append(closeport, port)
}
}
sort.Ints(openport)
fmt.Println(openport)
wg.Wait()
close(ports) //关闭 port
}