refactor(discovery): use ping for discovery and fill MAC via SSH
This commit is contained in:
@@ -1,61 +1,88 @@
|
||||
package discovery
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"auto-check/pkg/model"
|
||||
)
|
||||
|
||||
// Scanner 网段扫描器(串行,按顺序逐个探测)
|
||||
// Scanner 网段扫描器(并发 ping 探测)
|
||||
type Scanner struct {
|
||||
CIDR string
|
||||
Timeout time.Duration
|
||||
CIDR string
|
||||
Timeout time.Duration // 单 IP 探测超时
|
||||
Concurrency int // 并发数,默认 30
|
||||
}
|
||||
|
||||
// NewScanner 创建扫描器
|
||||
func NewScanner(cidr string, timeout time.Duration) *Scanner {
|
||||
func NewScanner(cidr string, timeout time.Duration, concurrency int) *Scanner {
|
||||
if concurrency <= 0 {
|
||||
concurrency = 10
|
||||
}
|
||||
return &Scanner{
|
||||
CIDR: cidr,
|
||||
Timeout: timeout,
|
||||
CIDR: cidr,
|
||||
Timeout: timeout,
|
||||
Concurrency: concurrency,
|
||||
}
|
||||
}
|
||||
|
||||
// Scan 扫描网段,返回存活设备列表(含 MAC 地址)
|
||||
// 串行逐个探测,每个 IP 有超时保护
|
||||
// Scan 并发 ping 扫描网段,返回存活设备列表(仅含 IP,MAC 留空待 SSH 阶段回填)
|
||||
func (s *Scanner) Scan() []model.Device {
|
||||
ips, ok := s.candidateIPs()
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
fmt.Printf("[扫描] 网段 %s,共 %d 个IP,开始探测...\n", s.CIDR, len(ips))
|
||||
fmt.Printf("[扫描] 网段 %s,共 %d 个IP,%d 并发 ping 探测...\n", s.CIDR, len(ips), s.Concurrency)
|
||||
|
||||
var results []model.Device
|
||||
for _, ip := range ips {
|
||||
if dev, ok := s.probe(ip); ok {
|
||||
results = append(results, dev)
|
||||
}
|
||||
jobs := make(chan string, len(ips))
|
||||
results := make(chan model.Device, len(ips))
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < s.Concurrency; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for ip := range jobs {
|
||||
if s.ping(ip) {
|
||||
results <- model.Device{IP: ip, MAC: ""}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
fmt.Printf("[扫描] 完成,发现 %d 台存活设备\n", len(results))
|
||||
return results
|
||||
for _, ip := range ips {
|
||||
jobs <- ip
|
||||
}
|
||||
close(jobs)
|
||||
|
||||
wg.Wait()
|
||||
close(results)
|
||||
|
||||
var devices []model.Device
|
||||
for dev := range results {
|
||||
devices = append(devices, dev)
|
||||
}
|
||||
|
||||
fmt.Printf("[扫描] 完成,发现 %d 台存活设备\n", len(devices))
|
||||
return devices
|
||||
}
|
||||
|
||||
// candidateIPs 解析网段并生成候选 IP 列表(排除网络地址和广播地址)
|
||||
func (s *Scanner) candidateIPs() ([]net.IP, bool) {
|
||||
ip, ipnet, err := net.ParseCIDR(s.CIDR)
|
||||
func (s *Scanner) candidateIPs() ([]string, bool) {
|
||||
_, ipnet, err := net.ParseCIDR(s.CIDR)
|
||||
if err != nil {
|
||||
fmt.Printf("[扫描] 网段解析失败: %v\n", err)
|
||||
return nil, false
|
||||
}
|
||||
var ips []net.IP
|
||||
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
|
||||
dst := make(net.IP, len(ip))
|
||||
copy(dst, ip)
|
||||
ips = append(ips, dst)
|
||||
ip := ipnet.IP.Mask(ipnet.Mask)
|
||||
var ips []string
|
||||
for ; ipnet.Contains(ip); inc(ip) {
|
||||
ips = append(ips, ip.String())
|
||||
}
|
||||
if len(ips) > 2 {
|
||||
ips = ips[1 : len(ips)-1]
|
||||
@@ -63,39 +90,47 @@ func (s *Scanner) candidateIPs() ([]net.IP, bool) {
|
||||
return ips, true
|
||||
}
|
||||
|
||||
// probe 探测单个 IP,存活则返回设备信息(MAC 通过 ARP 表查询)
|
||||
func (s *Scanner) probe(ip net.IP) (model.Device, bool) {
|
||||
if !s.isAlive(ip) {
|
||||
return model.Device{}, false
|
||||
// ping 探测单个 IP 是否存活(ICMP,经系统 ping 命令)。
|
||||
// 仅凭 exit code 不可靠(Windows 在某些代答/虚拟网卡场景会返回 0),
|
||||
// 因此解析输出,要求至少收到 1 个回包。
|
||||
func (s *Scanner) ping(ip string) bool {
|
||||
// ping 命令的等待超时(毫秒):固定 1s 足够局域网探测
|
||||
const pingWait = 1000
|
||||
// context 超时设为 ping 等待的 2 倍,留足 ping 自然退出时间,
|
||||
// 避免 context 先于 ping -w 杀进程导致拿不到输出。
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*pingWait*time.Millisecond)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "ping", "-n", "1", "-w", fmt.Sprintf("%d", pingWait), ip)
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
mac := lookupMAC(ip.String())
|
||||
desc := "无MAC"
|
||||
if mac != "" {
|
||||
desc = mac
|
||||
}
|
||||
fmt.Printf(" [+] %s 存活 (MAC: %s)\n", ip, desc)
|
||||
return model.Device{IP: ip.String(), MAC: mac}, true
|
||||
return parsePingReply(string(out))
|
||||
}
|
||||
|
||||
// isAlive 用 ping 探测单个 IP 是否存活(内置超时保护)
|
||||
func (s *Scanner) isAlive(ip net.IP) bool {
|
||||
waitMs := s.Timeout.Milliseconds()
|
||||
if waitMs < 1000 {
|
||||
waitMs = 1000
|
||||
// parsePingReply 判断 ping 输出是否表示存活
|
||||
func parsePingReply(out string) bool {
|
||||
for _, line := range strings.Split(out, "\n") {
|
||||
l := strings.ToLower(strings.TrimSpace(line))
|
||||
// Windows: "Packets: Sent = 1, Received = 1, Lost = 0"
|
||||
if strings.Contains(l, "received =") {
|
||||
i := strings.Index(l, "received =")
|
||||
n := strings.TrimSpace(l[i+len("received ="):])
|
||||
// 取数字前缀
|
||||
cnt := 0
|
||||
for _, c := range n {
|
||||
if c < '0' || c > '9' {
|
||||
break
|
||||
}
|
||||
cnt = cnt*10 + int(c-'0')
|
||||
}
|
||||
return cnt >= 1
|
||||
}
|
||||
}
|
||||
var args []string
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
args = []string{"-n", "1", "-w", fmt.Sprintf("%d", waitMs), ip.String()}
|
||||
case "darwin":
|
||||
args = []string{"-c", "1", "-W", fmt.Sprintf("%d", waitMs), ip.String()}
|
||||
default:
|
||||
args = []string{"-c", "1", "-W", fmt.Sprintf("%d", waitMs/1000), ip.String()}
|
||||
}
|
||||
return exec.Command("ping", args...).Run() == nil
|
||||
return false
|
||||
}
|
||||
|
||||
// inc IP 递增
|
||||
// inc IP 递增(原地修改,调用方负责复制)
|
||||
func inc(ip net.IP) {
|
||||
for j := len(ip) - 1; j >= 0; j-- {
|
||||
ip[j]++
|
||||
|
||||
Reference in New Issue
Block a user