Created
June 9, 2018 11:28
-
-
Save lnsp/fb637c1c917e7572e821466e65684f3a to your computer and use it in GitHub Desktop.
Simple network tool to list interfaces
This file contains 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" | |
"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