一、TCP扫描器
package mainimport ("flag""fmt""net""sync""time"
)func main() {hostname := flag.String("hostname", "www.baidu", "hostname to test")startPort := flag.Int("start-port", 80, "the port on which the scanning starts")endPort := flag.Int("end-port", 100, "the port from which the scanning ends")timeout := flag.Duration("timeout", time.Millisecond*200, "timeout")flag.Parse()ports := make([]int, 0)//同步机制wg := &sync.WaitGroup{}for port := *startPort; port <= *endPort; port++ {wg.Add(1)go func(p int) {opened := isOpen(*hostname, p, *timeout)if opened {ports = append(ports, p)}wg.Done()}(port)}wg.Wait()fmt.Printf("opened ports: %vn", ports)/**opened ports: [80]*/
}/**
检测端口是否可连接
*/
func isOpen(host string, port int, timeout time.Duration) bool {time.Sleep(time.Millisecond * 1)conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", host, port), timeout)if err == nil {_ = conn.Close()return true}return false
}
本文发布于:2024-01-29 07:47:03,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170648562513780.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |