This article illustrates a simple network port scanning method for go implementation. Share to everyone for your reference. The implementation methods are as follows:
Copy Code code as follows:
Package Main
Import (
"NET"
"FMT"
"OS"
"Runtime"
"Time"
"StrConv"
)
Func Loop (startport, Endport int, inport Chan int) {
For i: = Startport; I <= Endport; i++{
InPort <-I
}
}
Func Scanner (InPort, outport, out Chan int, IP net. IP, Endport int) {
for{
In: = <-inport
Fmt. Println (in)
TCPADDR: = &net. Tcpaddr{ip,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 () {
StartTime: = time. Now (). Unix ()
Runtime. Gomaxprocs (4)
InPort: = make (chan int)
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)
}
Startport, _:= StrConv. Atoi (OS. ARGS[2])
Endport, _: = StrConv. Atoi (OS. ARGS[3])
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: = <-outport
If Port!= 0{
Collect = append (collect, port)
}
}
}
}
I hope this article will help you with your go language program.