3

When you connect to your iPhone's personal hotspot, your WiFi icon changes from the signal strength "V" to a chain link. Such as in the image copied from another StackExchange question below:

Image of a linked wifi

I've written a script which can figure out what network interface is being used (out of the 14 or so that I get back from the route, ifconfig, and networksetup commands), but nothing that I can see in those would help differentiate a regular WiFi from a hotspot connection. The best I've been able to do so far is presume that if I'm on WiFi and the network gateway is 172.20.*, then I know it must be my iPhone.

I'm wondering to determine if I'm on a hotspot since the Mac obviously knows because of the icon it is using for the connection.

Can a program or script tell this on macOS Ventura 13.2.1?

bmike
  • 235,889
bjb
  • 2,396
  • You could start with the command networksetup -getairportnetwork enX where X is the identifier of the hardware device (i.e. en0 is WiFi on a MacBook Pro). – Allan Mar 24 '23 at 20:46
  • I'm curious what the end goal here is - you already know the status of the network since you have joined the network and see the icon in the menu bar - basically - what next? – bmike Mar 25 '23 at 18:25

2 Answers2

3

The network gateway approach is probably the simplest. I cannot find any output in any of the common commands to indicate if it is a hotspot.

As discussed in this question there is property isPersonalHotspot that is part of the CoreWLAN API.

I have written a simple Python3 script to use that API:

#!/usr/bin/env python3
from CoreWLAN import CWInterface

interface = CWInterface.interface() network = interface.lastNetworkJoined() if network: if network.isPersonalHotspot(): print(f"SSID: {network.ssid()}, is a personal hotspot.") else: print(f"SSID: {network.ssid()}, is not a personal hotspot.") else: print(f"Not connected to WiFi.")

To run this script you need to install python3, as well as pip3 install pyobjc-framework-CoreWLAN.

The script could also run python2, but since is was removed in MacOS 12.3 it is best to use python3.

thewade
  • 143
1

Another approach if you don't want to use CoreWLAN is parse the output of airport -s --xml.

If you haven't already you can add a symlink to airport with: sudo ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/local/bin/airport

The output contains all nearby networks, so you will have to find the network you are interested in among the results.

If the network you're interested in has the property IOS_IE it is likely a personal hotspot.

e.g.

      <key>IOS_IE</key>
      <dict>
         <key>IOS_IE_FEATURES</key>
         <data>AQAA</data>
         <key>IOS_IE_FEATURE_VERSION</key>
         <integer>1</integer>
         <key>IOS_IE_FEATURE_WOW_DISALLOWED</key>
         <true />
      </dict>

A user on github used this approach to check for tethering.

However this approach would be very crude and could lead to false detections, as the Information Element could be used for other things such as CarPlay.

I have posted a question on reverseengineering.stackexchange.com to see if we can better understand how the Information Element works.

But if this approach works for your needs, even with the chance of false detections, I decided to offer it here as well.

thewade
  • 143