Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // InetInfo gathers information about different net interfaces and their
- // IP addresses and returns that as a string for debug purposes.
- // Returns an error string if errors occurred.
- func InetInfo() string {
- // Loop over all the interfaces
- ifaces, err := net.Interfaces()
- if err != nil {
- return fmt.Sprintf("Error retrieving interfaces: %v", err.Error())
- }
- // This will map interface name to textual representations of its
- // IP addresses
- addrMap := map[string][]string{}
- for _, i := range ifaces {
- // Loop over all addresses for that interface
- addrs, err := i.Addrs()
- if err != nil {
- return fmt.Sprintf("Error retrieving addresses: %v", err.Error())
- }
- addrList := make([]string, 0)
- for _, addr := range addrs {
- var ip net.IP
- switch v := addr.(type) {
- case *net.IPAddr:
- ip = v.IP
- case *net.IPNet:
- ip = v.IP
- default:
- // Skip others
- continue
- }
- addr := ""
- if ipv4 := ip.To4(); ipv4 != nil {
- addr = fmt.Sprintf(" IPv4 addr: %v", ip.String())
- } else {
- addr = fmt.Sprintf(" IPv6 addr: %v", ip.String())
- }
- addrList = append(addrList, addr)
- }
- if len(addrList) > 0 {
- addrMap[i.Name] = addrList
- }
- }
- s := ""
- for ifName, addrList := range addrMap {
- s += fmt.Sprintf("\n\nInterface: %v", ifName)
- for _, a := range addrList {
- s += fmt.Sprintf("\n %v", a)
- }
- }
- return strings.TrimLeft(s, "\n ")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement