“Airplane Mode” in OS X. Alternative command for `rfkill`?

Mac OS X doesn't recognize rfkill from its terminal, and I am looking for an alternative command that has similar functionality (toggling "Airplane Mode").

NOTE: I don't wish to open an .app to toggle. I'd like to execute the command from command line or within script in Bash (or equivalents).


By "Airplane Mode", I meant to borrow the term from iOS support page here. I would like to disable all incoming/outgoing network communications from my system.


Solution 1:

Make an ad hoc location named isolated or airplane where all the network interfaces are present and turned off.

Switch to this deaf location with:

networksetup -switchtolocation isolated

It might be more interesting than rfkill since you can protect you from an unnoticed Ethernet cable which might be listening to all your communications (and they are a lot) :[. To check that everything is really off, run:

ifconfig -a | grep '[<,]UP[,>]' | grep -v '[<,]LOOPBACK[,>]'

If this command doesn't display anything, then all your interfaces are down, everything is OK.

If this command output anything, as here:

en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500

then it means that the interface name (here en1 which is the wireless one on this Mac) is on where it shouldn't. This is the sign that either the configuration made is failing or that some Trojan horse turned it on to reach its control server via the Internet.

Solution 2:

Depending on my objective (Should I concern about shutting down data interface as well?), I can choose the plan accordingly:

Plan A for (1) Network Interfaces and (2) Data Interface

Plan B for (1) Network Interfaces


(Plan A #1) Get installed hardware interfaces info (including data interfaces):

First, I'd like to know how many and what types of interfaces I should worry about.

$ networksetup -listallhardwareports

Hardware Port: Bluetooth DUN
Device: Bluetooth-Modem
Ethernet Address: N/A

Hardware Port: Ethernet
Device: en0
Ethernet Address: ff:ff:ff:ff:ff:ff

Hardware Port: FireWire
Device: fw0
Ethernet Address: ff:ff:ff:ff:ff:ff:ff:ff

Hardware Port: Wi-Fi
Device: en1
Ethernet Address: ff:ff:ff:ff:ff:f

(Plan A #?) (Optional) Get the list of network interfaces only:

This step makes me easy to grep.

$ networksetup -listallnetworkservices

An asterisk (*) denotes that a network service is disabled.
Ethernet
FireWire
Wi-Fi

(Plan A #2) Get the device name of the network interfaces.

I'd like to know which network interface(s) is/are active.

$ ifconfig

en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether ff:ff:ff:ff:ff:ff 
    media: autoselect (none)
    status: inactive
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether ff:ff:ff:ff:ff:ff 
    inet6 ffff::ffff:ffff:ffff:ffff%en1 prefixlen 64 scopeid 0x5 
    inet 192.168.1.fff netmask 0xffffff00 broadcast 192.168.1.fff
    media: autoselect
    status: active
fw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 4078
    lladdr ff:ff:ff:ff:ff:ff:ff:ff 
    media: autoselect <full-duplex>
    status: inactive

(Plan B #1) Locate airport command or set alias for airport command if not recognized:

My system couldn't recognize airport command, so I had to add below line to .bash_profile.

alias airport='/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport'

(Plan B #2) Check current AirPort status:

$ ./airport -I

     agrCtlRSSI: -10
     agrExtRSSI: 0
    agrCtlNoise: -10
    agrExtNoise: 0
          state: running
        op mode: station 
     lastTxRate: 100
        maxRate: 100
lastAssocStatus: 0
    802.11 auth: open
      link auth: fffffffffffffffff
          BSSID: ff:ff:ff:ff:ff:ff
           SSID: MelvNetwork
            MCS: 15
        channel: 6 

(Plan A & B #3)Turning on/off the AirPort:

# For OS X 10.7 and later
$ networksetup -setairportpower en1 on   # Turn on Airport
$ networksetup -setairportpower en1 off  # Turn off Airport

# for OS X 10.6 and below
$ networksetup -setairportpower on       # Turn on AirPort
$ networksetup -setairportpower off      # Turn off AirPort

(Plan A & B #4)Turning on/off the BlueTooth:

Download blueutil here.

$ blueutil status  # Check status of BlueTooth
Status: on

$ blueutil on      # Turn BlueTooth on

$ blueutil off     # Turn BlueTooth off

Script:

Now that I have all the ingredients, I can write the script for OS X 10.7!


Last Comments:

(1) Some of the output are masked with the character 'f'.

(2) According to $ man networksetup, you can configure the proxy, TCP/IP, PPPoE as well (not tested).

(3) @RichHomolka's suggestion, networksetup --enableuserprofile is deprecated for OS X 10.7 (not tested in other versions).

(4) If anyone following the above procedure encounters a problem, could you please address them in the comment (or as in a separate answer)?