|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/binary" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "net" |
| 9 | + "sync" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +var icmp ICMP |
| 14 | + |
| 15 | +var wg sync.WaitGroup |
| 16 | + |
| 17 | +type ICMP struct { |
| 18 | + Type uint8 |
| 19 | + Code uint8 |
| 20 | + Checksum uint16 |
| 21 | + Identifier uint16 |
| 22 | + SequenceNum uint16 |
| 23 | +} |
| 24 | + |
| 25 | +func main() { |
| 26 | + wg.Add(1) |
| 27 | + go CheckIP() |
| 28 | + CheckIP() |
| 29 | + wg.Wait() |
| 30 | +} |
| 31 | + |
| 32 | +func CheckIP() { |
| 33 | + defer wg.Done() |
| 34 | + var use []string |
| 35 | + var notuse []string |
| 36 | + t := time.Now() |
| 37 | + hosts, _ := hosts("10.150.1.1/24") |
| 38 | + for _, ip := range hosts { |
| 39 | + tmp := ip |
| 40 | + go func(data string) { |
| 41 | + bool := ping(data) |
| 42 | + if bool { |
| 43 | + use = append(use, data) |
| 44 | + fmt.Println("已使用IP:", use) |
| 45 | + } else { |
| 46 | + notuse = append(notuse, data) |
| 47 | + fmt.Println("未使用IP:", notuse) |
| 48 | + } |
| 49 | + }(tmp) |
| 50 | + } |
| 51 | + wg.Wait() |
| 52 | + elapsed := time.Since(t) |
| 53 | + fmt.Println("IP扫描完成,耗时", elapsed) |
| 54 | +} |
| 55 | + |
| 56 | +func hosts(cidr string) ([]string, error) { |
| 57 | + ip, ipnet, err := net.ParseCIDR(cidr) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + |
| 62 | + var ips []string |
| 63 | + for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) { |
| 64 | + ips = append(ips, ip.String()) |
| 65 | + } |
| 66 | + return ips[1 : len(ips)-1], nil |
| 67 | +} |
| 68 | + |
| 69 | +func inc(ip net.IP) { |
| 70 | + for j := len(ip) - 1; j >= 0; j-- { |
| 71 | + ip[j]++ |
| 72 | + if ip[j] > 0 { |
| 73 | + break |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func ping(ip string) bool { |
| 79 | + //开始填充数据包 |
| 80 | + icmp.Type = 8 //8->echo message 0->reply message |
| 81 | + icmp.Code = 0 |
| 82 | + icmp.Checksum = 0 |
| 83 | + icmp.Identifier = 0 |
| 84 | + icmp.SequenceNum = 0 |
| 85 | + |
| 86 | + recvBuf := make([]byte, 32) |
| 87 | + var buffer bytes.Buffer |
| 88 | + |
| 89 | + //先在buffer中写入icmp数据报求去校验和 |
| 90 | + binary.Write(&buffer, binary.BigEndian, icmp) |
| 91 | + icmp.Checksum = CheckSum(buffer.Bytes()) |
| 92 | + //然后清空buffer并把求完校验和的icmp数据报写入其中准备发送 |
| 93 | + buffer.Reset() |
| 94 | + binary.Write(&buffer, binary.BigEndian, icmp) |
| 95 | + |
| 96 | + Time, _ := time.ParseDuration("1s") |
| 97 | + conn, err := net.DialTimeout("ip4:icmp", ip, Time) |
| 98 | + if err != nil { |
| 99 | + return false |
| 100 | + } |
| 101 | + _, err = conn.Write(buffer.Bytes()) |
| 102 | + if err != nil { |
| 103 | + log.Println("conn.Write error:", err) |
| 104 | + return false |
| 105 | + } |
| 106 | + conn.SetReadDeadline(time.Now().Add(time.Second * 2)) |
| 107 | + num, err := conn.Read(recvBuf) |
| 108 | + if err != nil { |
| 109 | + return false |
| 110 | + } |
| 111 | + |
| 112 | + conn.SetReadDeadline(time.Time{}) |
| 113 | + |
| 114 | + if string(recvBuf[0:num]) != "" { |
| 115 | + return true |
| 116 | + } |
| 117 | + return false |
| 118 | + |
| 119 | +} |
| 120 | + |
| 121 | +func CheckSum(data []byte) uint16 { |
| 122 | + var ( |
| 123 | + sum uint32 |
| 124 | + length int = len(data) |
| 125 | + index int |
| 126 | + ) |
| 127 | + for length > 1 { |
| 128 | + sum += uint32(data[index])<<8 + uint32(data[index+1]) |
| 129 | + index += 2 |
| 130 | + length -= 2 |
| 131 | + } |
| 132 | + if length > 0 { |
| 133 | + sum += uint32(data[index]) |
| 134 | + } |
| 135 | + sum += (sum >> 16) |
| 136 | + |
| 137 | + return uint16(^sum) |
| 138 | +} |
0 commit comments