Skip to content

networksetup

networksetup – configuration tool for network settings in System Preferences.

list all network services

networksetup -listallnetworkservices

get current active network connection

#!/bin/sh
networksetup -listallnetworkservices | while read -r service; do
  if [ "$service" != "*" ]; then
    status=$(networksetup -getinfo "$service" | grep "IP address" | awk '{print $3}')
    if [ -n "$status" ] && [ "$status" != "none" ]; then
      echo "$service"
      break
    fi
  fi
done