package main
import ( "bufio" "fmt" "net" "os" "strconv" "strings" "sync" "time" )
func main() { file, err := os.Open("ips.txt") if err != nil { panic(err) } defer file.Close()
var wg sync.WaitGroup
scanner := bufio.NewScanner(file)
for scanner.Scan() { fields := strings.Split(scanner.Text(), ":") if len(fields) != 2 { fmt.Println("本行数据格不正确:", scanner.Text()) continue } ip := fields[0] port := fields[1]
if _, err := strconv.Atoi(port); err != nil { fmt.Println("端口数据不正确:", port) continue } wg.Add(1) go func(ip, port string, wg *sync.WaitGroup) { defer wg.Done() conn, err := net.DialTimeout("tcp", ip+":"+port, 5*time.Second) if err != nil { fmt.Printf("IP: %s, 端口: %s 已关闭.\n", ip, port) return } defer conn.Close() fmt.Printf("IP: %s, 端口: %s 是打开着的.\n", ip, port)
}(ip, port, &wg) } wg.Wait() }
|
评论
Twikoo V1.6.31