Skip to content

Instantly share code, notes, and snippets.

@polynomialspace
Created January 7, 2022 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save polynomialspace/940e4cc90ccd4858a81ac140853f97b9 to your computer and use it in GitHub Desktop.
Save polynomialspace/940e4cc90ccd4858a81ac140853f97b9 to your computer and use it in GitHub Desktop.
`ip a` but only the parts you care about
package main
import (
"fmt"
"net"
"os"
"text/tabwriter"
)
func main() {
interfaces, err := net.Interfaces()
if err != nil {
panic(err)
}
w := tabwriter.NewWriter(os.Stdout, 4, 8, 4, ' ', 0)
defer w.Flush()
for _, iface := range interfaces {
addrs, err := iface.Addrs()
if err != nil {
fmt.Fprintf(w, "%s:\t%v\n", iface.Name, err)
continue
}
if len(addrs) >= 1 {
fmt.Fprintf(w, "%s:\t%s\n", iface.Name, addrs[0])
for _, addr := range addrs[1:] {
fmt.Fprintf(w, "\t%s\n", addr)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment