這篇文章為大家帶來了關於Go的相關知識,其中主要跟大家介紹Go怎麼實現端口掃描器,有代碼示例,感興趣的朋友下面一起來看一下吧,希望對大家有幫助。
利用GO 批次掃描伺服器連接埠
1、連接埠掃描器V1 - 基本操作
package mainimport ( "fmt" "net" "time" "unsafe")func main() { tcpScan("127.0.0.1", 1, 65535)}func tcpScan(ip string, portStart int, portEnd int) { start := time.Now() // 参数校验 isok := verifyParam(ip, portStart, portEnd) if isok == false { fmt.Printf("[Exit]\n") } for i := portStart; i 65535 { fmt.Println("[Error] port is must in the range of 1~65535") return false } fmt.Printf("[Info] port start:%d end:%d \n", portStart, portEnd) return true}
2、連接埠掃描器V2 - 使用goroutine
package mainimport ( "fmt" "net" "sync" "time" "unsafe")func main() { tcpScanByGoroutine("127.0.0.1", 1, 65535)}func tcpScanByGoroutine(ip string, portStart int, portEnd int) { start := time.Now() // 参数校验 isok := verifyParam(ip, portStart, portEnd) if isok == false { fmt.Printf("[Exit]\n") } var wg sync.WaitGroup for i := portStart; i 65535 { fmt.Println("[Error] port is must in the range of 1~65535") return false } fmt.Printf("[Info] port start:%d end:%d \n", portStart, portEnd) return true}
3、連接埠掃描器V3 - 利用Goroutine Channel
package mainimport ( "fmt" "net" "sync" "time" "unsafe")func main() { tcpScanByGoroutineWithChannel("127.0.0.1", 1, 65535)}func handleWorker(ip string, ports chan int, wg *sync.WaitGroup) { for p := range ports { address := fmt.Sprintf("%s:%d", ip, p) conn, err := net.Dial("tcp", address) if err != nil { fmt.Printf("[info] %s Close \n", address) wg.Done() continue } conn.Close() fmt.Printf("[info] %s Open \n", address) wg.Done() }}func tcpScanByGoroutineWithChannel(ip string, portStart int, portEnd int) { start := time.Now() // 参数校验 isok := verifyParam(ip, portStart, portEnd) if isok == false { fmt.Printf("[Exit]\n") } ports := make(chan int, 100) var wg sync.WaitGroup for i := 0; i 65535 { fmt.Println("[Error] port is must in the range of 1~65535") return false } fmt.Printf("[Info] port start:%d end:%d \n", portStart, portEnd) return true}
4 連接埠掃描器V4 - 引入兩個Channel
// packagepackage mainimport ( "fmt" "net" "sort" "time" "unsafe")func main() { tcpScanByGoroutineWithChannelAndSort("127.0.0.1", 1, 65535)}// The function handles checking if ports are open or closed for a given IP address.func handleWorker(ip string, ports chan int, results chan int) { for p := range ports { address := fmt.Sprintf("%s:%d", ip, p) conn, err := net.Dial("tcp", address) if err != nil { // fmt.Printf("[debug] ip %s Close \n", address) results 0 { openSlice = append(openSlice, resPort) } else { closeSlice = append(closeSlice, -resPort) } } // 关闭 chan close(ports) close(results) // 排序 sort.Ints(openSlice) sort.Ints(closeSlice) // 输出 for _, p := range openSlice { fmt.Printf("[info] %s:%-8d Open\n", ip, p) } // for _, p := range closeSlice { // fmt.Printf("[info] %s:%-8d Close\n", ip, p) // } cost := time.Since(start) fmt.Printf("[tcpScanByGoroutineWithChannelAndSort] cost %s second \n", cost)}func verifyParam(ip string, portStart int, portEnd int) bool { netip := net.ParseIP(ip) if netip == nil { fmt.Println("[Error] ip type is must net.ip") return false } fmt.Printf("[Info] ip=%s | ip type is: %T | ip size is: %d \n", netip, netip, unsafe.Sizeof(netip)) if portStart 65535 { fmt.Println("[Error] port is must in the range of 1~65535") return false } fmt.Printf("[Info] port start:%d end:%d \n", portStart, portEnd) return true}
以上是一文詳解Go如何實現連接埠掃描器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Go語言的核心特性包括垃圾回收、靜態鏈接和並發支持。 1.Go語言的並發模型通過goroutine和channel實現高效並發編程。 2.接口和多態性通過實現接口方法,使得不同類型可以統一處理。 3.基本用法展示了函數定義和調用的高效性。 4.高級用法中,切片提供了動態調整大小的強大功能。 5.常見錯誤如競態條件可以通過gotest-race檢測並解決。 6.性能優化通過sync.Pool重用對象,減少垃圾回收壓力。

Go語言在構建高效且可擴展的系統中表現出色,其優勢包括:1.高性能:編譯成機器碼,運行速度快;2.並發編程:通過goroutines和channels簡化多任務處理;3.簡潔性:語法簡潔,降低學習和維護成本;4.跨平台:支持跨平台編譯,方便部署。

關於SQL查詢結果排序的疑惑學習SQL的過程中,常常會遇到一些令人困惑的問題。最近,筆者在閱讀《MICK-SQL基礎�...

golang ...

Go語言中如何對比並處理三個結構體在Go語言編程中,有時需要對比兩個結構體的差異,並將這些差異應用到第�...

GoLand中自定義結構體標籤不顯示怎麼辦?在使用GoLand進行Go語言開發時,很多開發者會遇到自定義結構體標籤在�...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3漢化版
中文版,非常好用

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

禪工作室 13.0.1
強大的PHP整合開發環境