Skip to content

Instantly share code, notes, and snippets.

@lnsp
Created June 9, 2018 11:28
Show Gist options
  • Save lnsp/fb637c1c917e7572e821466e65684f3a to your computer and use it in GitHub Desktop.
Save lnsp/fb637c1c917e7572e821466e65684f3a to your computer and use it in GitHub Desktop.
Simple network tool to list interfaces
package main
import (
"fmt"
"github.com/fatih/color"
"net"
"os"
)
func main() {
hwaddr := color.New(color.FgRed, color.BgBlack).SprintFunc()
netproto := color.New(color.FgWhite, color.BgBlack).SprintFunc()
netaddr := color.New(color.FgGreen, color.BgBlack).SprintFunc()
ifaces, err := net.Interfaces()
if err != nil {
fmt.Fprintln(os.Stderr, "failed to get interfaces:", err)
os.Exit(1)
}
for _, iface := range ifaces {
// Print out hardware address
if iface.Flags == 0 {
continue
}
fmt.Printf("%-12s %-24s %s\n", iface.Name, iface.Flags, hwaddr(iface.HardwareAddr))
// Print out network address
addrs, err := iface.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
fmt.Printf("%12s %6s %s\n", "", netproto(addr.Network()), netaddr(addr.String()))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment