Reputation:
I am looking for a way to programmatically toggle the WiFi state of my laptop using a script. I am using MacOS (formally OSx).
Upvotes: 1
Views: 148
Reputation: 7732
I have an alias in my .zshrc
:
alias wifi:on="networksetup -setairportpower en2 on"
alias wifi:off="networksetup -setairportpower en2 off”
You need to change en2
to the correct network adapter on your machine.
You can list all adapters using:
networksetup -listallhardwareports
You can also extract this logic to its own script or function that receives the network-id
as a variable.
> ./toggle_wifi --id=2
Update
This question was also asked apple.stackexchange and has a few well received answers. There is also a Gist of a working version of the script.
Upvotes: 1