Go语言实现的简单网络端口扫描方法
本文实例讲述了go语言实现的简单网络端口扫描方法。分享给大家供大家参考。具体实现方法如下:
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)
}
}
}
}
希望本文所述对大家的go语言程序设计有所帮助。
上一篇: ubuntu下在docker中安装mysql5.6 的方法
下一篇: Java项目中常见的文件夹名称