AppleScript - determine the network service to which the current IP address is bound

3k Views Asked by At

The following AppleScript code will return all the network services in the Network Setup.

tell application "System Events"
        tell current location of network preferences
            set names to get name of every service
        end tell
 end tell

How do I get only the active network service name instead of all?

Update with clarification: 'active' in this context means the network service to whose interface the current [primary] IPv4 address is bound.

2

There are 2 best solutions below

4
On BEST ANSWER

@mcgrailm's answer shows you how to obtain all active (read: not disabled) network services, whether currently bound to addresses or not.

This answer, by contrast, shows you how to determine the network service to whose interface the current (primary) IPv4 address is bound:

Unfortunately, this is non-trivial, as the information must be drawn from several sources:

  • The services objects (from dictionary System Events.sdef) contains no information about currently bound addresses.
  • However, a service object's interface property contains a MAC address property.
  • While IPv4 address of (system info) returns the current (primary) IPv4 address, sadly, primary Ethernet address of (system info) always returns the wired Ethernet interface's MAC address, which may or may not be the interface to which the current IPv4 address is bound.
  • Thus, to determine the MAC address corresponding to the current IPv4 address, the solution below uses do shell script to parse the output from CLI ifconfig with help from awk.
# Obtain the [network] `service` instance underlying the 
# current IPv4 address...
set serviceWithPrimaryIpv4Address to my networkServiceByIp4Address("")
# ... and extract the name.
set nameOfServiceWithPrimaryIpv4Address to name of serviceWithPrimaryIpv4Address

# ---- Helper handlers.

# SYNOPSIS
#   networkServiceByIp4Address([addr])
# DESCRIPTION
#   Given (one of) the local system's IPv4 address(es), returns the network service object whose interface
#   the address is bound to (class `network service` is defined in `Sytem Events.sdef`).
#   If `addr` is an empty string or a missing value, the current primary IPv4 address is used.
#  An error is thrown if no matching service is found.
on networkServiceByIp4Address(addr)
  local macAddress
  set macAddress to my ip4ToMacAddress(addr)
  tell application "System Events"
    try
      tell current location of network preferences
        return first service whose (MAC address of interface of it) is macAddress
      end tell
    end try
  end tell
  error "No network service found matching IP address '" & addr & "'." number 500
end networkServiceByIp4Address

# SYNOPSIS
#   ip4ToMacAddress([addr])
# DESCRIPTION
#   Given (one of) the local system's IPv4 address(es), returns the corresponding network interface's
#   MAC address (regardless of interface type).
#   If `addr` is an empty string or a missing value, the current primary IPv4 address is used.
#  An error is thrown if no matching MAC address is found.
on ip4ToMacAddress(addr)
  if addr as string is "" then set addr to IPv4 address of (system info)
  try
    do shell script "ifconfig | awk -v ip4=" & ¬
      quoted form of addr & ¬
      " 'NF==2 && $2 ~ /^[a-f0-9]{2}(:[a-f0-9]{2}){5}$/ {macAddr=$2; next} $1 == \"inet\" && $2 == ip4 {print macAddr; exit}'"
    if result ≠ "" then return result
  end try
  error "No MAC address found matching IP address '" & addr & "'." number 500
end ip4ToMacAddress

my ip4ToMacAddress("")
3
On

You just need to include it in your query :)

tell application "System Events"
    tell current location of network preferences
        set names to get name of every service whose active is true
    end tell
end tell