Skip to content

Instantly share code, notes, and snippets.

@knice
Created February 12, 2013 15:58
Show Gist options
  • Select an option

  • Save knice/4770886 to your computer and use it in GitHub Desktop.

Select an option

Save knice/4770886 to your computer and use it in GitHub Desktop.
Look for and connect to WiFi network.
#!/bin/bash
## This script checks to see if the Mac is connected to
## a particular network. If it isn't, the script shuts off
## the wifi radio and turns it back on, then attempts
## to connect to the specified network.
##
## You can run this script at regular intervals with
## an app called Lingon, which utilizes launchd.
## Just make sure to chmod -x the script first.
network="YOUR_NETWORK_NAME"
password="YOUR_NETWORK_PASSWORD"
confirmStr="Current Wi-Fi Network: "$network
networkTest=`networksetup -getairportnetwork en1`
if [ "$networkTest" = "$confirmStr" ]
then
echo "You are on the expected network"
else
networksetup -setairportpower en1 off
networksetup -setairportpower en1 on
networksetup -setairportnetwork en1 $network $password
echo `networksetup -getairportnetwork en1`
fi
@knice

knice commented Feb 12, 2013

Copy link
Copy Markdown
Author

I created this because I'm having issues with a laptop driving a public display on a flaky WiFi network. I don't have control over the flaky WiFi and I don't have easy access to the building to deal with issues that can't be solived via remote access (like, say, flaky WiFi).

I plan to set the laptop to automatically sleep at regular intervals and when it wakes up, I'd like for it to check it's internet connection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment