Created
January 7, 2022 10:41
-
-
Save polynomialspace/940e4cc90ccd4858a81ac140853f97b9 to your computer and use it in GitHub Desktop.
`ip a` but only the parts you care about
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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