Skip to content

Commit

Permalink
add -i switch for single interface name
Browse files Browse the repository at this point in the history
  • Loading branch information
jftuga committed Jul 31, 2024
1 parent 1b3afe9 commit dde88a0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
11 changes: 8 additions & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
version: 2

before:
hooks:
- go mod tidy
- go generate ./...
- go test

builds:
- id: nics-id1
- id: nics
binary: nics
ldflags:
- -extldflags "-static" -s -w -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser -X main.Version={{.Version}} -X main.Revision={{.ShortCommit}}
Expand Down Expand Up @@ -32,7 +36,7 @@ builds:
- goos: darwin
goarch: ppc64le

- id: nics-id2
- id: nics-win
binary: nics
ldflags:
- -extldflags "-static" -s -w -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser -X main.Version={{.Version}} -X main.Revision={{.ShortCommit}}
Expand Down Expand Up @@ -73,10 +77,11 @@ brews:
repository:
owner: jftuga
name: homebrew-tap
token: "{{ .Env.HOMEBREW_TOKEN }}"
commit_author:
name: jftuga
email: jftuga@users.noreply.github.com
homepage: https://github.com/jftuga/nics
description: "Display information about Network Interface Cards (NICs)"
description: "nics: Display information about Network Interface Cards (NICs)"
test: system "#{bin}/nics -v"
install: bin.install "nics"
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/jftuga/nics

go 1.21.1
go 1.22.5

require (
github.com/olekukonko/tablewriter v0.0.5
golang.org/x/net v0.23.0
golang.org/x/net v0.27.0
)

require github.com/mattn/go-runewidth v0.0.9 // indirect
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/Qd
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
31 changes: 24 additions & 7 deletions nics.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/olekukonko/tablewriter"
)

const version = "1.4.6"
const version = "1.5.0"

func isBriefEntry(ifaceName, macAddr, mtu, flags string, ipv4List, ipv6List []string, debug bool) bool {
if debug {
Expand Down Expand Up @@ -81,17 +81,23 @@ func extractIPAddrs(ifaceName string, allAddresses []net.Addr, brief bool) ([]st
return allIPv4, allIPv6
}

func networkInterfaces(brief bool, debug bool) ([]string, []string) {
func networkInterfaces(brief bool, debug bool, singleInterface string) ([]string, []string) {
adapters, err := net.Interfaces()
if err != nil {
fmt.Print(fmt.Errorf("%+v\n", err.Error()))
return nil, nil
}

foundSingleInterface := false
if len(singleInterface) > 0 {
brief = false
singleInterface = strings.ToLower(singleInterface)
}

table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
if brief {
table.SetHeader([]string{"Name", "IPv4", "Mac Address", "MTU", "Flags"})
table.SetHeader([]string{"Name", "IP", "Mac Address", "MTU", "Flags"})
} else {
table.SetHeader([]string{"Name", "IPv4", "IPv6", "Mac Address", "MTU", "Flags"})
}
Expand All @@ -113,16 +119,21 @@ func networkInterfaces(brief bool, debug bool) ([]string, []string) {
fmt.Println(iface.Name, allAddresses)
fmt.Println("ipv4:", allIPv4)
fmt.Println("ipv6:", allIPv6)

}

ifaceName := strings.ToLower(iface.Name)
if len(singleInterface) > 0 && ifaceName != singleInterface {
continue
} else if len(singleInterface) > 0 && ifaceName == singleInterface {
foundSingleInterface = true
}
macAddr := iface.HardwareAddr.String()
mtu := strconv.Itoa(iface.MTU)
flags := iface.Flags.String()

if brief && isBriefEntry(ifaceName, macAddr, mtu, flags, allIPv4, allIPv6, debug) {
table.Append([]string{iface.Name, strings.Join(allIPv4, "\n"), macAddr, mtu, flags})
joined := strings.Join(allIPv4, "\n") // + "\n" + strings.Join(allIPv6, "\n")
table.Append([]string{iface.Name, joined, macAddr, mtu, flags})
for _, ipWithMask := range allIPv4 {
ip := strings.Split(ipWithMask, "/")
v4Addresses = append(v4Addresses, ip[0])
Expand All @@ -144,7 +155,11 @@ func networkInterfaces(brief bool, debug bool) ([]string, []string) {
}
}
}
table.Render()
if len(singleInterface) > 0 && !foundSingleInterface {
_, _ = fmt.Fprintf(os.Stderr, "\ninterface not found: %v\n", singleInterface)
} else {
table.Render()
}

return v4Addresses, v6Addresses
}
Expand All @@ -153,6 +168,8 @@ func main() {
argsAllDetails := flag.Bool("a", false, "show all details on ALL interfaces, including DHCP")
argsDebug := flag.Bool("d", false, "show debug information")
argsVersion := flag.Bool("v", false, "show program version")
argsSingleInterface := flag.String("i", "", "interface name")

flag.Usage = func() {
pgmName := os.Args[0]
if strings.HasPrefix(os.Args[0], "./") {
Expand All @@ -170,7 +187,7 @@ func main() {
os.Exit(1)
}

allIPv4, allIPv6 := networkInterfaces(!(*argsAllDetails), *argsDebug)
allIPv4, allIPv6 := networkInterfaces(!(*argsAllDetails), *argsDebug, *argsSingleInterface)
fmt.Println()
gatewayAndDNS(allIPv4, allIPv6, !(*argsAllDetails))
}

0 comments on commit dde88a0

Please sign in to comment.