- 学了GO不玩一下那不是很浪费 写了一个端口扫描器 大家可以一起学习下
package main
import (
"flag"
"fmt"
"net"
"runtime"
"time"
)
func main() {
cores := runtime.NumCPU()
runtime.GOMAXPROCS(cores - 1)
domainP := flag.String("domain", "", "请输入指定的IP地址")
flag.Parse()
domain := *domainP
if domain == "" {
fmt.Println("请输入IP地址")
return
}
openPort := make([]int, 10)
exitChan := make(chan bool)
portChan := make(chan int)
resChan := make(chan int, 100)
go func() {
for i := 0; i < 65535; i++ {
portChan <- i
}
close(portChan)
}()
timeout := time.Millisecond * 200
for i := 0; i < 18; i++ {
go CheckIsOpen(domain, timeout, portChan, exitChan, openPort, resChan)
}
for i := 0; i < 18; i++ {
<-exitChan
}
close(exitChan)
close(resChan)
fmt.Println("开启的端口号:")
for {
port, res := <-resChan
if !res {
break
}
fmt.Println(port)
}
}
func CheckIsOpen(domain string, timeout time.Duration, portChan chan int, exitChan chan bool, openPort []int, reschan chan int) {
for {
port, ok := <-portChan
if !ok {
break
}
socket := fmt.Sprintf("%v:%v", domain, port)
_, err := net.DialTimeout("tcp", socket, timeout)
defer func() {
recover()
}()
if err == nil {
reschan <- port
fmt.Println(socket, " 打开")
openPort = append(openPort, port)
} else {
fmt.Println(socket, " 关闭")
}
}
exitChan <- true
}