Golang Port Scan Example

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

This code is mainly from the network, the source code has problems, has now been fixed, can be directly compiled to run


Package Mainimport ("NET" "FMT" "OS" "Runtime" "Time" "StrConv") func loops (startport, Endport int, inport Chan int) {for I: = St Artport; I <= Endport; I++{inport <-I}}func Scanner (InPort, outport, out Chan int, IP net. IP, Endport int) {for{in: = <-inporttcpaddr: = &net. Tcpaddr{ip:ip,port:in}conn, err: = Net. DIALTCP ("TCP", nil, tcpaddr) if err! = Nil {outport <-0}else{outport <-in Conn. Close ()}if in = = Endport{out <-in}}}func Main () {runtime. Gomaxprocs (4) InPort: = make (chan int) StartTime: = time. Now (). Unix () Outport: = make (chan int) out: = Make (chan int) Collect: = []int{}if len (OS). Args)! = 4 {fmt. Println ("Usage:scanner.exe IP startport endport") fmt. PRINTLN ("Endport must be larger than Startport") os. Exit (0)}ip: = Net. PARSEIP (OS. ARGS[1]) if (OS. ARGS[3] < OS. Args[2]) {FMT. Println ("Usage:scanner IP startport endport") fmt. PRINTLN ("Endport must be larger than Startport") os. Exit (0)}fmt. Printf ("The IP is%s \ r \ n", IP) startport, _:= StrConv. Atoi (OS. ARGS[2]) Endport, _: = StrcoNv. Atoi (OS. ARGS[3]) fmt. Printf ("%d---------%d\r\n", Startport,endport) go Loop (Startport, Endport, InPort) for{select {case <-out:fmt. PRINTLN (collect) Endtime: = time. Now (). Unix () fmt. PRINTLN ("The scan process has spent", Endtime-starttime, "second") OS. Exit (0) Default:go scanner (InPort, outport, out, IP, endport) port: = <-outportif Port! = 0{collect = Append (Collect, PO RT)}}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.