How Can I Detect When I Have an Ethernet Cable Connected?

Given the Network Preferences, and AppleScript support for that, I thought this should be straight forward. But I can’t find an AppleScript solution, even though I’ve searched extensively.

I need a handler/function that returns true/false to isEthernetConnected()

Here’s what it looks like in Network Preferences:

CONNECTED:

.
NOT CONNECTED:

.

But I do NOT see this “Status” field/property in the Network Preferences service item.

@ccstone suggested this:

ifconfig -a

and then search for

media: autoselect (100baseTX ) status: active

That almost works, but I had to change it to “1000baseT”.

But I’d really like to have a direct AppleScript or ASObjC solution, if there is one.

Any ideas?

Test Script

tell application "System Events"
  
  tell network preferences
    --- no help here ---
    set netProp to properties
  end tell
  
  tell current location of network preferences
    
    set serviceList to every service -- WORKS
    
    --- this works, but don't see any properties that indicate status ---
    set enetService to service "Thunderbolt Ethernet Slot 1"
    
    --- this is "missing value" ---
    set enetConfig to current configuration of enetService
    
    --- Unfortunately this is not cleared when the cable is disconnected ---
    set macAddr to MAC address of interface of enetService
    
  end tell
end tell

RESULTS

By my reading of System Events’ dictionary, it should be the connected property of a configuration. But with current configuration returning missing value and no configurations showing up as elements of services, my guess is that it has been removed deliberately.

There’s no ASObjC option either; the APIs are all C-based.

If it’s just for your own use, you can probably get something out of ifconfig -au – the -u limits it to connections that are up.

This works here, but obviously you’ll need a wider test base.

on isEtherNetConnected()
	set t to do shell script "system_profiler SPNetworkDataType | grep -i 'media subtype'"
	if t contains "none" then
		return "False"
	else
		return "True"
	end if
	
end isEtherNetConnected


set f to isEtherNetConnected()

EDIT: though I may have misunderstood your need. This will test whether an ethernet device is connected to the mac (i.e, the ethernet cable is plugged in), but on reflection I expect you want to know if the ethernet connection to the internet is working. You’ll probably need a separate test for that, such as a ping to google.com or the like.

Phil, you understood my need perfectly.

Thanks for your script, but unfortunately, it does not work for me. It returns true even when I disconnect the Ethernet cable.

It looks like you are looking for a match with “media subtype” from the system_profiler output. That returns three matches of “media subtype”, none of which are actually connected.

Here is an example output. I do NOT have a “Thunderbolt FireWire” attached, although the dock I’m using has a FW port.

    Thunderbolt FireWire:

      Type: FireWire
      Hardware: FireWire
      BSD Device Name: fw0
      IPv4:
          Configuration Method: DHCP
      IPv6:
          Configuration Method: Automatic
      Ethernet:
          MAC Address: 00:50:b6:10:00:00:5d:31
          Media Options: Full Duplex
          Media Subtype: Auto Select
      Proxies:
          Exceptions List: *.local, 169.254/16
          FTP Passive Mode: Yes
      Service Order: 5

Thanks again for your help.

Thanks Shane. I think that may be the ticket.
It provides an output that includes this:

en8: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=b<RXCSUM,TXCSUM,VLAN_HWTAGGING>
	ether 00:50:b6:ca:f0:dc 
	inet6 fe80::250:b6ff:feca:f0dc%en8 prefixlen 64 scopeid 0x9 
	inet 192.168.1.15 netmask 0xffffff00 broadcast 192.168.1.255
	nd6 options=1<PERFORMNUD>
	media: autoselect (1000baseT <full-duplex,flow-control>)
	status: active

The status: active becomes status: inactive when the cable is disconnected.

I’m working on a RegEx to parse the results, but this should work.
The en\d: and ether identify the Ethernet connections.

Almost all connections will have an ether parameter. even virtual ones created by VMware fusion have a MAC address, which is what the ether parameter shows.

in general, the first en* listing is the ethernet port, especially on machines with a dedicated ethernet port. So on my laptop, that’s en0.

on a machine with two, IIRC, those are en0 and en1.

If you want to be sure, you can do a combination of commands. First, assuming all you care about are en* interfaces, you could get a list of all en* interfaces:

ifconfig -au|grep -c en.:

which will return the number of en0/en1 type interfaces.

then a loop that iterates through those applying them to the netstat command:

netstat -S -I en0|grep lqm

That will return a “good” (literally) when the interface is up and off when it is down/no cable plugged in:

netstat -S -I en1|grep lqm
lqm: “good” availability: "true"
netstat -S -I en0|grep lqm
lqm: “good” availability: "true"
netstat -S -I en1|grep lqm
lqm: “off” availability: "true"
netstat -S -I en0|grep lqm
lqm: “off” availability: “true”

it’s an additional command, but it may be easier to grab the results from netstat than ifconfig.

1 Like