Added logic to find next available IP address

This commit is contained in:
Mike Conrad
2025-04-14 15:50:08 -04:00
parent 2964391f0a
commit d0e3d6777d
5 changed files with 159 additions and 4 deletions

42
configreader.go Normal file
View File

@ -0,0 +1,42 @@
package main
import (
"fmt"
"io/fs"
"net"
"path/filepath"
"strings"
"gopkg.in/ini.v1"
)
func parseUsedIPsFromConfigs(dir string) ([]net.IP, error) {
var usedIPs []net.IP
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() || !strings.HasSuffix(d.Name(), ".conf") {
return nil
}
cfg, err := ini.Load(path)
if err != nil {
return fmt.Errorf("failed to parse config %s: %w", path, err)
}
// Check [Interface] section
if iface := cfg.Section("Interface"); iface != nil {
if addr := iface.Key("Address").String(); addr != "" {
ip := strings.Split(addr, "/")[0]
usedIPs = append(usedIPs, net.ParseIP(ip))
}
}
return nil
})
if err != nil {
return nil, err
}
return usedIPs, nil
}