2

I read a few stacks on the internet but command line was used only to turn Wi-Fi on or off, but none of them to check the on-off status of Wi-Fi.

Any possible way to check status of Wi-Fi adapter on or off in command line?

8

You can use the ifconfig command, e.g.:

$ ifconfig en0 
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether # Removed for Security. 
    inet6 fe80::c44:6ce5:5d57:5b93%en0 prefixlen 64 secured scopeid 0x9 
    inet 192.168.2.101 netmask 0xffffff00 broadcast 192.168.2.255
    nd6 options=201<PERFORMNUD,DAD>
    media: autoselect
    status: active
$

You can also filter the output, e.g.:

$ ifconfig en0 | awk '/status:/{print $2}'
active
$ 

When not active the output is, e.g:

$ ifconfig en0 | awk '/status:/{print $2}'
inactive
$

If you do not know the device name for the Wi-Fi network adapter, you can use the following examples:

$ networksetup -getairportpower $(system_profiler SPAirPortDataType | awk -F: '/Interfaces:/{getline; print $1;}')
Wi-Fi Power (en0): On
$

Or:

$ ifconfig $(system_profiler SPAirPortDataType | awk -F: '/Interfaces:/{getline; print $1;}') | awk '/status:/{print $2}'
active
$
9
  • 1
    nicely done, thanks mate
    – laughing
    Sep 2 '19 at 2:41
  • @laughing Just note that if you have multiple network interfaces, you'll need to determine which of your network interfaces are your Wifi adapter. For example, on my mid-2010 MacBook Pro, wireless is en1. YMMV. You can use 'ifconfig' on its own to list all your adapters. Sep 2 '19 at 3:52
  • @TraneFrancks so instead of specifying the interface, is there any command line that returns the interface of Wi-Fi adapter? my goal is just to control the Wi-Fi adapter.
    – laughing
    Sep 2 '19 at 4:09
  • 1
    @ankiiiiiii, system_profiler SPAirPortDataType | awk -F: '/Status:/{print $2}' with return either Connected or Off. Sep 2 '19 at 4:55
  • 1
    The airport command itself also allows for some wifi control, but its not in the standard shell path Sep 2 '19 at 5:44
5

Actually, networksetup has the command to return the status of airportpower as well.

networksetup -getairportpower *specify the interface*

For example:

networksetup -getairportpower en0
2

In addition to ifconfig and networksetup, there's also the airport command. It's in an obscure location, so you have to specify the entire path to it (or make an alias, like I have). I find the -I (show current status) and -s (scan for networks) options most useful (although they're considered "legacy"). Here's an example:

$ alias airport=/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport
$ airport -I
AirPort: Off
$ # [Switches radio on...]
$ airport -I
     agrCtlRSSI: -49
     agrExtRSSI: 0
    agrCtlNoise: -88
    agrExtNoise: 0
          state: running
        op mode: station 
     lastTxRate: 300
        maxRate: 300
lastAssocStatus: 0
    802.11 auth: open
      link auth: wpa2-psk
          BSSID: 88:d7:f6:25:c4:37
           SSID: NotMyRealName
            MCS: 15
        channel: 40,-1

You can use the -h flag to get a list of its options.

0

The way to simply get the device name is to use the listallhardwareports option of networksetup. It neatly prints out all the device names for each interface immediately (e.g. it does not need to poll the device status and take a couple seconds to return like system_profiler does).

Building off of user3439894's answer, the command to programmatically get the "Wi-Fi" adapter name would be:

networksetup -getairportpower $(networksetup -listallhardwareports | awk -F: '/Wi-Fi/{getline; print $2;}')

This can be used for any adapter name (not just Wi-Fi), and could easily be paramaterized in a script, e.g.

getDeviceNameFromAdapterName() {
    networksetup -listallhardwareports | awk -v adapter="$1" -F : '$0~adapter{getline; print $2;}'
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .