Go language implementation of Port scanner sharing _golang

Source: Internet
Author: User
Tags sleep sprintf

Copy Code code as follows:

Go Language Implementation port scan
Defects
Port cannot be set to global variable I don't know how to set it.
var L = list. New () This is an array operation, not a Message Queuing function like Message Queuing

Implementation features
Implementing Generation IP Segments
Implementing port Scanning
Implement parameter Pass in
Write file to Local
Main.go 58.215.20.30 58.215.201.30 80
File name start IP End IP Scan port
QQ29295842 hope to know more friends technical exchange
QQ group 367196336 Go Golang web Security Development
Package Main

    Import (
        "Container/list"
         "FMT"
        "NET"
         "OS"
        "StrConv"
         "Strings"
        "Time"
   )

    func ip2num (IP string) int {
        cansplit: = Func (c Rune) bool {return c = = '. '}
        lisit: = Strings. Fieldsfunc (IP, cansplit)//[58 215]
       //fmt. Println (lisit)
        Ip1_str_int, _: = StrConv. Atoi (lisit[0])
        Ip2_str_int, _: = StrConv. Atoi (lisit[1])
        Ip3_str_int, _: = StrConv. Atoi (lisit[2])
        Ip4_str_int, _: = StrConv. Atoi (lisit[3])
        return ip1_str_int<<24 | ip2_str_int<<16 | ip3_str_int<<8 | Ip4_str_int
   }

    func num2ip (num int) string {
        ip1_int: = (num & 0xff000000) >>
        ip2_int: = (num & 0x00ff0000) >>
        ip3_int: = (num & 0x0000ff00) >> 8
    & nbsp;   ip4_int: = num & 0x000000ff
       //fmt. Println (ip1_int)
        data: = FMT. Sprintf ("%d.%d.%d.%d", Ip1_int, Ip2_int, Ip3_int, ip4_int)
        return Data
   }

    func gen_ip (Aip1 int, Aip2 int) {
        index: = Aip1
        for index < AIP2 {
            //fmt. PRINTLN (NUM2IP (index))
           //Team, press stack
             Ip_data: = Num2ip (index)
            //fmt. Println (ip_data)
            l.pushback (ip_data)
            index++
        }
   }

Func text_add (name string, data string) {//write data to File Text_add ("File2.txt", "QQQQQQQQQQQQQQQQQQQQQQQ")
F, err: = OS. OpenFile (name, OS. O_rdwr|os. O_create|os. O_append, 0x644)
If Err!= nil {
Panic (ERR)
}
Defer F.close ()

_, Err = f.writestring (data)
_, Err = F.writestring ("\ r \ n")
If Err!= nil {
Panic (ERR)
}
}

Text_add ("File2.txt", "QQQQQQQQQQQQQQQQQQQQQQQ")
var L = list. New ()

Func socket_ip (host string, port string) bool {
VAR (
Remote = host + ":" + port
)

Tcpaddr, _: = Net. RESOLVETCPADDR ("TCP4", Remote)//Convert IP format
Fmt. Printf ("%s", tcpaddr)
Conn, Err: = Net. DIALTCP ("TCP", nil, tcpaddr)//See if connection is successful
If Err!= nil {
Fmt. Printf ("no==%s:%s\r\n", host, Port)
return False

}
Defer Conn. Close ()
Fmt. Printf ("ok==%s:%s\r\n", host, Port)
return True
}

Func for_ip (port string) {
Now: = time. Now ()
Year, Mon, day: = Now. UTC (). Date ()
file_name: = Fmt. Sprintf ("%d-%d-%d_%s", Year, Mon, day, port)
For {//Dead loop
If L.len () <= 0 {
Fmt. Println ("Jump out of the loop")
Break//#跳出
}
The team was previously read
I1: = L.front ()
L.remove (I1)
IP, _: = I1. Value. (string)
If SOCKET_IP (IP, port) {
Ok
Gets the current date as the file name in the Write IP
Text_add (file_name+ "_ok.txt", IP)
}//else {
Text_add (file_name+ "_no.txt", IP)
// }

Time. Sleep (Time.millisecond * 500)/NA second unit
}
}

Func Main () {
Argslen: = Len (OS. Args)
Fmt. Println (Argslen)
If Argslen!= 4 {
Fmt. Println ("Main.go 58.215.20.30 58.215.201.30 80")
} else {
GEN_IP (Ip2num (OS). Args[1]), Ip2num (OS. ARGS[2]))
For index: = 0; Index < 200; index++ {
Go for_ip (OS. ARGS[3])
}
for {
Time. Sleep (1 * time. Second)//nanosecond as Unit
}

}
}

Related Article

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.