├── .github ├── dependabot.yml └── workflows │ └── main.yml ├── README.md ├── go.mod ├── go.sum └── iface.go /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: gomod 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | time: "10:00" 8 | open-pull-requests-limit: 10 9 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | schedule: 11 | - cron: "0 9 * * *" 12 | 13 | jobs: 14 | build: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@v1 18 | - name: Build 19 | run: go build -o iface -v . 20 | - name: Run 21 | run: ./iface 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # iface 2 | > Network interface command-line utility. 3 | 4 | Tools like `ifconfig`, `ip` and `ipconfig` are awesome! But, sometimes they're just a little too much to parse -- or you just don't want to fuss with all of that. With `iface`, you get a clean, scriptable command-line utility that's lightweight, cross-platform, and easy to learn. Learn once, use everywhere! 5 | 6 | ## Supported Platforms 7 | 8 | > Thanks to [`VBaczynski`](https://github.com/VBaczynski) for helping test FreeBSD! 9 | 10 | * macOS 11 | * Linux 12 | * Windows 13 | * FreeBSD 14 | 15 | ## Install 16 | 17 | ``` 18 | $ go get github.com/picatz/iface 19 | ``` 20 | 21 | ## Build from Source 22 | 23 | Until I've figured out a better solution: 24 | 25 | ``` 26 | $ go get github.com/urfave/cli 27 | $ git clone https://github.com/picatz/iface.git 28 | $ cd iface 29 | $ go build iface.go 30 | ``` 31 | 32 | Build `iface.exe` on macOS or Linux to run on a Windows box: 33 | 34 | ``` 35 | $ GOOS=windows GOARCH=386 go build iface.go 36 | ``` 37 | 38 | ## Example Usage 39 | 40 | Get the first `default` ( non-[loopback](https://en.wikipedia.org/wiki/Loopback#Virtual_network_interface) ) network [interface](https://en.wikipedia.org/wiki/Network_interface): 41 | > This sort of simulates the LibPcap [`lookupdev`](https://linux.die.net/man/3/pcap_lookupdev) function to get the default interface to capture packets on. 42 | 43 | ``` 44 | $ iface default 45 | ``` 46 | 47 | Get all of the network interface names available on the system: 48 | 49 | ``` 50 | $ iface names 51 | ``` 52 | 53 | Get network interface names with thier [mac addresses](https://en.wikipedia.org/wiki/MAC_address): 54 | 55 | ``` 56 | $ iface macs 57 | ``` 58 | 59 | Get network interface names with their [IP addresses](https://en.wikipedia.org/wiki/IP_address): 60 | 61 | ``` 62 | $ iface ips 63 | ``` 64 | 65 | ## Command-line Interface 66 | 67 | ``` 68 | NAME: 69 | iface - network interface command-line utility 70 | 71 | USAGE: 72 | iface [global options] command [command options] [arguments...] 73 | 74 | VERSION: 75 | 1.0.1 76 | 77 | COMMANDS: 78 | names all network interface names 79 | macs network interfaces with their mac addresses 80 | mtus network interfaces with their maximum transmission unit (mtu)s 81 | ips network interfaces with their (local) ip addresses 82 | default the first non-loopback network interface 83 | help, h Shows a list of commands or help for one command 84 | 85 | GLOBAL OPTIONS: 86 | --help, -h show help 87 | --version, -v print the version 88 | ``` 89 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module iface 2 | 3 | go 1.12 4 | 5 | require github.com/urfave/cli v1.22.5 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 2 | github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= 3 | github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= 4 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 5 | github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= 6 | github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 7 | github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= 8 | github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= 9 | github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw= 10 | github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= 11 | github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY= 12 | github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= 13 | github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo= 14 | github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= 15 | github.com/urfave/cli v1.22.3 h1:FpNT6zq26xNpHZy08emi755QwzLPs6Pukqjlc7RfOMU= 16 | github.com/urfave/cli v1.22.3/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= 17 | github.com/urfave/cli v1.22.4 h1:u7tSpNPPswAFymm8IehJhy4uJMlUuU/GmqSkvJ1InXA= 18 | github.com/urfave/cli v1.22.4/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= 19 | github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU= 20 | github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= 21 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 22 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 23 | -------------------------------------------------------------------------------- /iface.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "net" 7 | "os" 8 | 9 | "github.com/urfave/cli" 10 | ) 11 | 12 | func main() { 13 | app := cli.NewApp() 14 | 15 | app.Name = "iface" 16 | app.Version = "1.0.1" 17 | app.Usage = "network interface command-line utility" 18 | 19 | app.Commands = []cli.Command{ 20 | { 21 | Name: "names", 22 | Usage: "all network interface names", 23 | Action: func(c *cli.Context) error { 24 | ifaces, err := net.Interfaces() 25 | if err != nil { 26 | log.Fatal(err) 27 | } 28 | for _, iface := range ifaces { 29 | fmt.Println(iface.Name) 30 | } 31 | return nil 32 | }, 33 | }, 34 | { 35 | Name: "macs", 36 | Usage: "network interfaces with their mac addresses", 37 | Action: func(c *cli.Context) error { 38 | ifaces, err := net.Interfaces() 39 | if err != nil { 40 | log.Fatal(err) 41 | } 42 | for _, iface := range ifaces { 43 | if iface.HardwareAddr != nil { 44 | fmt.Println(iface.HardwareAddr, iface.Name) 45 | } 46 | } 47 | return nil 48 | }, 49 | }, 50 | { 51 | Name: "mtus", 52 | Usage: "network interfaces with their maximum transmission unit (mtu)s", 53 | Action: func(c *cli.Context) error { 54 | ifaces, err := net.Interfaces() 55 | if err != nil { 56 | log.Fatal(err) 57 | } 58 | for _, iface := range ifaces { 59 | fmt.Println(iface.MTU, iface.Name) 60 | } 61 | return nil 62 | }, 63 | }, 64 | { 65 | Name: "ips", 66 | Usage: "network interfaces with their (local) ip addresses", 67 | Action: func(c *cli.Context) error { 68 | ifaces, err := net.Interfaces() 69 | if err != nil { 70 | log.Fatal(err) 71 | os.Exit(1) 72 | } 73 | for _, iface := range ifaces { 74 | addrs, err := iface.Addrs() 75 | if err != nil { 76 | log.Fatal(err) 77 | os.Exit(1) 78 | } 79 | for _, addr := range addrs { 80 | fmt.Println(iface.Name, addr) 81 | } 82 | } 83 | return nil 84 | }, 85 | }, 86 | { 87 | Name: "default", 88 | Usage: "the first non-loopback network interface", 89 | Action: func(c *cli.Context) error { 90 | ifaces, err := net.Interfaces() 91 | if err != nil { 92 | log.Fatal(err) 93 | } 94 | for _, iface := range ifaces { 95 | // must have mac address, FlagUp and FlagBroadcast 96 | if iface.HardwareAddr != nil && iface.Flags&net.FlagUp != 0 && iface.Flags&net.FlagBroadcast != 0 { 97 | fmt.Println(iface.Name) 98 | break 99 | } 100 | } 101 | return nil 102 | }, 103 | }, 104 | } 105 | 106 | app.Run(os.Args) 107 | } 108 | --------------------------------------------------------------------------------