Determine LAN/WAN Interfaces from CLI in Machine-Parseable Format

Started by foxpa.ws, June 25, 2024, 12:45:37 PM

Previous topic - Next topic
Hello!

I'm writing a script that I'd like to be agnostic about what interface names have been designated LAN or WAN so that it can be as portable as possible and not require the user to specify their interface(s) of choice by name, as this varies by driver and by order of initialization.

Is there a simple way for me to get that information in an easily-parsed format? I noticed the description field of ifconfig's output has what I need but as a sed n00b and it being on a separate line as the interface name I'm not sure if figuring out how to backtrack to it is worthwhile or if there is a better, cleaner way to ouput all interfaces of type x (WAN/LAN/OPT) so I can pipe it through a sed or regex and script with it more readily?

Perhaps there is a config file I should be grepping? I took a boo around /etc and am yet at a loss. Maybe an API call I can make from the cli or curl that spits out a little JSON?

Thanks!

netstat -ni --libxo json
Deciso DEC750
People who think they know everything are a great annoyance to those of us who do. (Isaac Asimov)

That's a pretty awesome insight and one I will definitely make use of in the future, thanks! Unfortunately, I do not see a way the interfaces can be differentiated by LAN/WAN/OPT designation. My output:
# netstat -ni --libxo json | json_pp
{
   "statistics" : {
      "interface" : [
         {
            "address" : "enc0",
            "collisions" : 0,
            "dropped-packets" : 0,
            "flags" : "0x0",
            "mtu" : 1536,
            "name" : "enc0",
            "network" : "<Link#1>",
            "received-errors" : 0,
            "received-packets" : 0,
            "send-errors" : 0,
            "sent-packets" : 0
         },
         {
            "address" : "lo0",
            "collisions" : 0,
            "dropped-packets" : 0,
            "flags" : "0x8049",
            "mtu" : 16384,
            "name" : "lo0",
            "network" : "<Link#2>",
            "received-errors" : 0,
            "received-packets" : 43106475,
            "send-errors" : 0,
            "sent-packets" : 43106475
         },
         {
            "address" : "::1",
            "flags" : "0x8049",
            "name" : "lo0",
            "network" : "::1/128",
            "received-packets" : 0,
            "sent-packets" : 0
         },
         {
            "address" : "fe80::1%lo0",
            "flags" : "0x8049",
            "name" : "lo0",
            "network" : "fe80::%lo0/64",
            "received-packets" : 0,
            "sent-packets" : 0
         },
         {
            "address" : "127.0.0.1",
            "flags" : "0x8049",
            "name" : "lo0",
            "network" : "127.0.0.0/8",
            "received-packets" : 43106475,
            "sent-packets" : 43106475
         },
         {
            "address" : "pfsync0",
            "collisions" : 0,
            "dropped-packets" : 0,
            "flags" : "0x0",
            "mtu" : 1500,
            "name" : "pfsync0",
            "network" : "<Link#3>",
            "received-errors" : 0,
            "received-packets" : 0,
            "send-errors" : 0,
            "sent-packets" : 0
         },
         {
            "address" : "pflog0",
            "collisions" : 0,
            "dropped-packets" : 0,
            "flags" : "0x20100",
            "mtu" : 33160,
            "name" : "pflog0",
            "network" : "<Link#4>",
            "received-errors" : 0,
            "received-packets" : 0,
            "send-errors" : 0,
            "sent-packets" : 0
         },
         {
            "address" : "fa:8f:4e:40:ee:b5",
            "collisions" : 0,
            "dropped-packets" : 0,
            "flags" : "0x8843",
            "mtu" : 1500,
            "name" : "xn0",
            "network" : "<Link#5>",
            "received-errors" : 0,
            "received-packets" : 4480553623,
            "send-errors" : 0,
            "sent-packets" : 2771648753
         },
         {
            "address" : "108.63.21.82",
            "flags" : "0x8843",
            "name" : "xn0",
            "network" : "108.63.21.80/29",
            "received-packets" : 1547762,
            "sent-packets" : 20687781
         },
         {
            "address" : "2e:3e:a9:7f:34:45",
            "collisions" : 0,
            "dropped-packets" : 0,
            "flags" : "0x8943",
            "mtu" : 1500,
            "name" : "xn1",
            "network" : "<Link#6>",
            "received-errors" : 0,
            "received-packets" : 3039114099,
            "send-errors" : 0,
            "sent-packets" : 4467667506
         },
         {
            "address" : "aaa.bbb.ccc.1",
            "flags" : "0x8943",
            "name" : "xn1",
            "network" : "aaa.bbb.ccc.0/24",
            "received-packets" : 4905395,
            "sent-packets" : 1711444
         },
         {
            "address" : "aaa.bbb.ccc.211",
            "flags" : "0x8943",
            "name" : "xn1",
            "network" : "aaa.bbb.ccc.0/24",
            "received-packets" : 429339,
            "sent-packets" : 612557
         }
      ]
   }
}

Looks like the interface description is in the output of ifconfig only - unfortunately that command has not yet been libxo-ified. So no JSON but hard to parse text output only.

Possibly you can combine the two. Use the JSON list from netstat to iterate over the interfaces and then pull the description from ifconfig. See `man ifconfig` for reference, please.

HTH,
Patrick
Deciso DEC750
People who think they know everything are a great annoyance to those of us who do. (Isaac Asimov)