6

Is it possible to have a bash script that toggle my wifi on and off? I am using lion (10.7.2).

3 Answers 3

7

Assuming your hardware is somewhat similar to mine:

networksetup -setairportpower en1 on
networksetup -setairportpower en1 off

You can run networksetup -listallhardwareports to find the device (en1) of your Wi-Fi in case it's different.

To toggle put something like the following lines into a script or shell function:

[[ "$(networksetup -getairportpower en1)" == *On ]] && v=off || v=on
networksetup -setairportpower en1 $v
2
#!/bin/bash

device=$(networksetup -listallhardwareports |
grep -E '(Wi-Fi|AirPort)' -A 1 | grep -o en.)
[[ "$(networksetup -getairportpower $device)" == *On ]] && v=off || v=on
networksetup -setairportpower $device $v

The port name is Wi-Fi on 10.7 and AirPort on 10.6. The device name can be at least en0, en1, or en2 depending on your setup.

0

Head over to this page. A guy by the name of heatxsink made a pretty handy script that will toggle your wifi on or off depending on the status of your ethernet connection.

You can add your own growl notifications pretty easily if you want. Also make sure to follow every step precisely as the script relies on filenames and locations of the files.

Other options: You can try out ControlPlane. It seems promising to fill your need for automation.

You must log in to answer this question.

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