main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "net"
  6. "strconv"
  7. "strings"
  8. )
  9. func generateIP(startIP, endIP string) (string, error) {
  10. start := net.ParseIP(startIP)
  11. end := net.ParseIP(endIP)
  12. if start == nil || end == nil {
  13. return "", fmt.Errorf("invalid IP address")
  14. }
  15. ip := incrementIP(start)
  16. if ip.Equal(end) {
  17. return "", fmt.Errorf("invalid IP address")
  18. }
  19. return ip.String(), nil
  20. }
  21. func incrementIP(ip net.IP) net.IP {
  22. ip = ip.To4()
  23. if ip == nil {
  24. return ip // Return if it's not a valid IPv4 address
  25. }
  26. for i := len(ip) - 1; i >= 0; i-- {
  27. if ip[i] < 255 {
  28. ip[i]++
  29. return ip
  30. }
  31. ip[i] = 0 // Reset to 0 if it overflows
  32. }
  33. return ip // Return the incremented IP
  34. }
  35. // checkIPInRange 检查给定 IP 是否在指定的范围内
  36. func checkIPInRange(ipStr, startStr, endStr string) (bool, error) {
  37. ip := net.ParseIP(ipStr)
  38. start := net.ParseIP(startStr)
  39. end := net.ParseIP(endStr)
  40. if ip == nil || start == nil || end == nil {
  41. return false, fmt.Errorf("invalid IP address")
  42. }
  43. return bytesCompare(ip, start) >= 0 && bytesCompare(ip, end) <= 0, nil
  44. }
  45. // bytesCompare 对 IP 进行字节比较
  46. func bytesCompare(a, b net.IP) int {
  47. return bytes.Compare(a, b)
  48. }
  49. func ipToInt(ip net.IP) int64 {
  50. // 将 IP 地址转换为整数
  51. return int64(ip[0])<<24 + int64(ip[1])<<16 + int64(ip[2])<<8 + int64(ip[3])
  52. }
  53. func generateSequentialMAC(start [6]byte, count int) [][6]byte {
  54. macAddresses := make([][6]byte, count)
  55. for i := 0; i < count; i++ {
  56. macAddresses[i] = start
  57. // 递增MAC地址
  58. for j := 5; j >= 0; j-- {
  59. if start[j]++; start[j] != 0 {
  60. break
  61. }
  62. }
  63. }
  64. return macAddresses
  65. }
  66. // parseMACString 将字符串格式的 MAC 地址转换为 [6]byte 类型
  67. func parseMACString(macStr string) ([6]byte, error) {
  68. macParts := strings.Split(macStr, ":")
  69. if len(macParts) != 6 {
  70. return [6]byte{}, fmt.Errorf("invalid MAC address format: %s", macStr)
  71. }
  72. var mac [6]byte
  73. for i, part := range macParts {
  74. num, err := strconv.ParseUint(part, 16, 8)
  75. if err != nil {
  76. return [6]byte{}, fmt.Errorf("invalid MAC address format: %s", macStr)
  77. }
  78. mac[i] = byte(num)
  79. }
  80. return mac, nil
  81. }
  82. func formatMAC(mac [6]byte) string {
  83. return fmt.Sprintf("%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5])
  84. }
  85. func main() {
  86. macStr := "23:E0:00:00:01:01"
  87. // 转换为 [6]byte
  88. startMAC, err := parseMACString(macStr)
  89. if err != nil {
  90. fmt.Println("Error:", err)
  91. return
  92. }
  93. if isValidMACAddressInRange(startMAC, 0) {
  94. fmt.Println("MAC地址在指定范围内")
  95. } else {
  96. fmt.Println("MAC地址不在指定范围内")
  97. }
  98. }
  99. func NowIP() string {
  100. return "192.168.1.13"
  101. }
  102. func GenerateIP(start, end string) string {
  103. //startIp := net.ParseIP(start)
  104. //endIp := net.ParseIP(end)
  105. //
  106. //ip := net.ParseIP(NowIP())
  107. return ""
  108. }
  109. // isValidMACAddressInRange 检查 MAC 地址是否在指定范围内
  110. func isValidMACAddressInRange(mac [6]byte, networkType int) bool {
  111. startMAC, err := parseMACString("23:E0:00:00:00:00")
  112. if err != nil {
  113. return false
  114. }
  115. endMAC, err := parseMACString("23:E0:00:00:00:FF")
  116. if err != nil {
  117. return false
  118. }
  119. for i := 0; i < 6; i++ {
  120. if mac[i] < startMAC[i] || mac[i] > endMAC[i] {
  121. return false
  122. }
  123. }
  124. return true
  125. }