List all Ethernet Interfaces

Jason
Contributor II

Is there a way in terminal to list all interfaces of a certain type? For example, In System Information under the Network category i can see a Type column which has values of AirPort, Ethernet, and PPP (PPPSerial). I'd like to be able to list out everything of type "Ethernet"

2 ACCEPTED SOLUTIONS

Samuarl
New Contributor
#/bin/sh
networksetup -listallnetworkservices | while read service;
do
     networksetup -getinfo "$service" | grep 'Ethernet Address' > /dev/null
     if [ $? -eq 0 ]
     then
        echo "$service"
     fi
done

This will show all network services which have an 'Ethernet Address' property. Surprisingly Bluetooth shows as an Ethernet type link. If you want to exclude it you can just add another ```
grep -v 'Bluetooth'
and for only active interfaces add
grep -v 'null'
```

View solution in original post

dbrodjieski
New Contributor III

While running system_profiler by itself is slow, you may get what you need by running

system_profiler SPNetworkDataType

Since you are only querying the Network data type, it's relatively quick and wouldn't bog down recon too much. Clean up the output of that, and that should give you the same info as System Information is providing.

EDIT: While it isn't the prettiest, this appears to get a clean list of all services of Type: Ethernet

system_profiler SPNetworkDataType | grep -B2 "Type: Ethernet" | awk 'BEGIN {FS=":"; RS="--"} { print $1 }' | sed -e 's/^ *//' -e '/^[ 	]*$/d'

View solution in original post

15 REPLIES 15

sgoetz
Contributor

This may not be totally what you want, but will get you close. This is what I use to turn off IPv6 on all interfaces.

networksetup -listallnetworkservices | sed "1 d" | while read output ;do networksetup -setv6off "$output";done

mm2270
Legendary Contributor III

Something like this? It might look a little sloppy under certain circumstances, but should get you mostly what you're looking for.

networksetup -listallhardwareports | grep -A2 "Hardware Port.*Ethernet"

As an example, I ran this on a new Mac with Thunderbolt, changed the "Ethernet" in the grep to "Thunderbolt" instead and it printed (I changed the MAC addresses before posting):

Hardware Port: Thunderbolt Ethernet
Device: en5
Ethernet Address: 00:00:35:c7:0e:a0
--
Hardware Port: Thunderbolt 1
Device: en1
Ethernet Address: 00:00:05:28:f9:b0
--
Hardware Port: Thunderbolt 2
Device: en2
Ethernet Address: 00:00:05:28:f9:b1
--
Hardware Port: Thunderbolt Bridge
Device: bridge0
Ethernet Address: N/A

Jason
Contributor II

Hi @sgoetz, That does give a list of all interfaces, but does not allow filtering out services that are not Ethernet

Hi @mm2270, that's getting closer, but if a USB Ethernet adapter were used instead then that wouldn't grab it. I've seen some scripts where they look for the friendly service name and filter on that, but that's assuming the service name didn't get renamed. I might end up using something like that and get 99.9% accuracy, but i was thinking if i could use the "Type" or "Hardware" attributes that System Information is getting, then i'd be at 100% accuracy. just not sure how to get at those with command line

mm2270
Legendary Contributor III

Actually it will list USB Ethernet, because its looking for the term "Ethernet" anywhere after the line Hardware Port" It doesn't matter if its the first word or the middle or the last. In fact, I just plugged in a USB Ethernet dongle to my Mac and re-ran the line and it showed up. (It listed Thunderbolt Ethernet and USB Ethernet)
[s]But... you're correct that if the service was renamed by the user and doesn't include that term it could get passed up. I don't have the time at the moment, but if I can, I'l see if there's some way to pull them no matter what they're named. It might not be possible though.[/s]
Correction - I just renamed my USB Ethernet to "USB ENet" and tried again and networksetup still reports it as "USB Ethernet" so it doesn't use the friendly name. The script line should pull anything with "Ethernet" in it, even if renamed.

sgoetz
Contributor

How about:

networksetup -listallnetworkservices | sed "1 d" | grep "Ethernet" | while read output ;do networksetup -setv6off "$output";done

Jason
Contributor II

@mm2270, yep you're correct. I think something like "system_profiler | grep -A1 "Hardware: Ethernet" might be close, although not very fast. I'd need to work on my string handling to clean it up so it outputs just the friendly name or BSD Device Name.

@sgoetz, that does list just services with the friendly names containing Ethernet, but users can change that as mentioned above. I don't believe networksetup contains details on the Type or Hardware to really know what the service is physically. I know I'm being super picky, but retrieving services with a particular hardware type and not just a matching user-defined service name is what i'm after.

mm2270
Legendary Contributor III

@Jason, read my correction above. Although I renamed my USB Ethernet connection in System Preferences to something that shouldn't have been picked up, it seems networksetup still sees it as the full name with "Ethernet" in it.
Now, that said, I have not rebooted since renaming the interface, so I'll need to try that later and see if it still works.

Also, I would avoid using system_profiler whenever possible. As you said, its slow, and if you planned on using that with an Extension Attribute, it would mean running that slow command at every recon.

nessts
Valued Contributor II

here is a perl way to get an ethernet and find out if its active.

sub getPorts  {
syslog('notice', "Getting list of hardware devices and their ports");
   open NET, "networksetup -listallhardwareports |"
       or die "$progname: listallhardwareports $!
";
   while (<NET>) {
       chomp;
       if (/^Hardware Port:/) {
           my $tmpname = (split ":")[1];
           $tmpname =~ s/^s*//;
           $tmpname =~ s/s*$//;
           my $tmpdev = <NET>;
           chomp $tmpdev;
           $tmpdev = (split ":",$tmpdev)[1];
           $tmpdev =~ s/^s*//;
           $tmpdev =~ s/s*$//;
           $ports{$tmpname} = $tmpdev;
           syslog('notice', "Port found %s: %s", $tmpname, $tmpdev);
       }
   }
   close NET;
}

sub isEthernetActive {
        my $isActive = "off";
        my $IP = undef;
        for (keys %ports) {
                next unless /Ethernet/;
                syslog('notice', "found interface %s", $_);
                my $nic = $ports{$_};
                open IFCONFIG, "ifconfig $nic |" or die "$progname: ifconfig $nic: $!
";
                while (<IFCONFIG>) {
                        next unless /inets/;
                        $IP = (split ' ', $_)[1];
                        last;
                }
                if (defined $IP) {
                        last;
                }
                else {
                        next;
                }
        }
        close IFCONFIG;
        if (defined $IP) {
                syslog('notice', "found IP address %s", $IP);
                $isActive = "on" unless ($IP =~ /^169./);
        }
        syslog('notice', "Ethernet state is %s", $isActive);
        return $isActive;
}

sean
Valued Contributor

Maybe this is what you are after:

#!/bin/bash

wifiPort=`printf "list" | scutil | grep -m 1 "AirPort" | awk -F "/" '{print $(NF-1)}'`

ifconfig | grep "^en" | grep -v "$wifiPort" | cut -d ":" -f 1 | while read line
do
        isActive=`printf "get State:/Network/Interface/${line}/Link
d.show" | scutil | grep Active | awk '{print $NF}'`
        echo "$line active: $isActive"
done

exit 0

Jason
Contributor II

@mm2270, It looks like a reboot does end that from working.

@nessts, this is also reliant on the user friendly name of the service. Renaming "Thunderbolt Ethernet" to "Enet" would skip over that service, even though it's still of type Ethernet.

@sean, this looks interesting. does it only show active ports? I'm going to test it soon.

Samuarl
New Contributor
#/bin/sh
networksetup -listallnetworkservices | while read service;
do
     networksetup -getinfo "$service" | grep 'Ethernet Address' > /dev/null
     if [ $? -eq 0 ]
     then
        echo "$service"
     fi
done

This will show all network services which have an 'Ethernet Address' property. Surprisingly Bluetooth shows as an Ethernet type link. If you want to exclude it you can just add another ```
grep -v 'Bluetooth'
and for only active interfaces add
grep -v 'null'
```

dbrodjieski
New Contributor III

While running system_profiler by itself is slow, you may get what you need by running

system_profiler SPNetworkDataType

Since you are only querying the Network data type, it's relatively quick and wouldn't bog down recon too much. Clean up the output of that, and that should give you the same info as System Information is providing.

EDIT: While it isn't the prettiest, this appears to get a clean list of all services of Type: Ethernet

system_profiler SPNetworkDataType | grep -B2 "Type: Ethernet" | awk 'BEGIN {FS=":"; RS="--"} { print $1 }' | sed -e 's/^ *//' -e '/^[ 	]*$/d'

Jason
Contributor II

@dbrodjieski, wow that works really well and executes fast. I just connected a Thunderbolt Dock yesterday and it shows up as "Thunderbolt Ethernet Slot 1". This picks up that as well as my Novatel Wireless 4G service (which is of type Ethernet as well). I think it would have taken me months to get the syntax with awk, grep, and sed working, if ever.

Jason
Contributor II

@Samuarl, this is interesting as well. It returned 5 of my 6 Ethernet devices. It didn't include "Thunderbolt Bridge" which @dbrodjieski 's solution included (even though it has a type of Ethernet). This could be preferred in some situations.

bentoms
Release Candidate Programs Tester

Hi All,

i'd steer clear of system_profiler as it can be a bit heavy.

I'd use something like I have here: http://macmule.com/2011/09/09/how-to-change-the-automatic-proxy-configuration-url-in-system-preferen...