Go語言實(shí)現(xiàn)的簡單網(wǎng)絡(luò)端口掃描方法
本文實(shí)例講述了Go語言實(shí)現(xiàn)的簡單網(wǎng)絡(luò)端口掃描方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
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)
}
}
}
}
希望本文所述對(duì)大家的Go語言程序設(shè)計(jì)有所幫助。
相關(guān)文章
使用Golang實(shí)現(xiàn)加權(quán)負(fù)載均衡算法的實(shí)現(xiàn)代碼
這篇文章主要介紹了使用Golang實(shí)現(xiàn)加權(quán)負(fù)載均衡算法的實(shí)現(xiàn)代碼,詳細(xì)說明權(quán)重轉(zhuǎn)發(fā)算法的實(shí)現(xiàn),通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Go語言中函數(shù)可變參數(shù)(Variadic Parameter)詳解
在Python中,在函數(shù)參數(shù)不確定數(shù)量的情況下,可以動(dòng)態(tài)在函數(shù)內(nèi)獲取參數(shù)。在Go語言中,也有類似的實(shí)現(xiàn)方式,本文就來為大家詳細(xì)講解一下2022-07-07go語言實(shí)現(xiàn)markdown解析庫的方法示例
這篇文章主要介紹了go語言實(shí)現(xiàn)markdown解析庫的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02