scan_port.go
package main
import (
"flag"
"fmt"
"net"
"os"
"strings"
"time"
)
func main() {
// 解析命令行参数
ipRange := flag.String("i", "", "IP range or single IP to scan, e.g., 192.168.1.0/24 or 192.168.1.1")
port := flag.String("p", "22", "Port number to scan, e.g., 22")
timeoutSec := flag.Int("t", 1, "Timeout for each connection attempt in seconds")
flag.Parse()
if *ipRange == "" {
fmt.Println("IP range or IP address is required.")
os.Exit(1)
}
timeout := time.Duration(*timeoutSec) * time.Second
// 检查是否是单个IP
if !strings.Contains(*ipRange, "/") {
scanIP(*ipRange, *port, timeout)
} else {
// 处理IP网段
ip, ipNet, err := net.ParseCIDR(*ipRange)
if err != nil {
fmt.Println("Invalid IP range:", err)
os.Exit(1)
}
for ip := ip.Mask(ipNet.Mask); ipNet.Contains(ip); incrementIP(ip) {
scanIP(ip.String(), *port, timeout)
}
}
}
func scanIP(ip, port string, timeout time.Duration) {
// 尝试使用Telnet连接到指定的IP和端口
address := net.JoinHostPort(ip, port)
conn, err := net.DialTimeout("tcp", address, timeout)
if err == nil {
fmt.Println("Active IP:", ip)
conn.Close()
}
}
func incrementIP(ip net.IP) {
// IP地址加一
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
编译:
D:\goscan> go build scan_port.go
扫描网段1521端口
D:\goscan>.\scan_port.exe -i 192.168.1.0/24 -p 1521 -t 1
Active IP: 192.168.1.11
Active IP: 192.168.1.12
Active IP: 192.168.1.13
Active IP: 192.168.1.18
Active IP: 192.168.1.23
Active IP: 192.168.1.24
Active IP: 192.168.1.26
Active IP: 192.168.1.27
Active IP: 192.168.1.28
Active IP: 192.168.1.62
Active IP: 192.168.1.65
Active IP: 192.168.1.74
Active IP: 192.168.1.78
Active IP: 192.168.1.79
Active IP: 192.168.1.87
Active IP: 192.168.1.88
Active IP: 192.168.1.95
Active IP: 192.168.1.96
扫单个IP端口
D:\goscan>.\scan_port.exe -i 192.168.1.15 -p 3306
Active IP: 192.168.1.15
「欢迎关注我们的公众号,获取更多技术分享与经验交流。」
文章转载自数据库驾驶舱,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




