Giter Club home page Giter Club logo

wifi-location-changer's Introduction

Mac OSX Wi-Fi Location Changer

  • Automatically changes the Mac OSX network location when a configured Wi-Fi (SSID) becomes connected
  • Allows having different IP settings depending on the Wi-Fi SSID
  • Offers hook to run external script when location changes

Configuration

Create a configuration file using the sample:

cp ./locationchanger.conf.sample ./locationchanger.conf

Add to this new file (./locationchanger.conf) a single line for each pair of location and SSID that you want this service to recognize and set when the SSID connects. That is, for each location, add one line with both a location name and a Wi-Fi SSID, separated by a space, taking care to use exact capitalization, and using quotations as necessary.

For example, if your location is "home", and the Wi-Fi SSID to trigger that location is "myWifiName", then a line in the configuration file would look like:

home myWifiName

If your SSID is instead a name like Wu Tang LAN, with spaces, then use quotes around the SSID like:

home "Wu Tang LAN"

Note: Ensure you use the exact location names as they appear under "Location" in OSX's System Preferences -> Network, and for SSIDs in your Wi-Fi menu. Capitalization must match! Spaces must match within a quoted name!

Add as many location + SSID lines as you like to the configuration file.

MacOS Notifications

The script triggers a MacOS Notification upon changing location. If you don't want this just delete the lines that start with osascript.

Installation

Automated Installation

Execute:

./install.sh

Manual Installation

Copy these files:

cp locationchanger /usr/local/bin
cp locationchanger.conf /usr/local/bin
cp LocationChanger.plist ~/Library/LaunchAgents/

Should you place the locationchanger script to another location, make sure you edit the path in LocationChanger.plist too.

Make locationchanger script executable:

chmod +x /usr/local/bin/locationchanger

Load LocationChanger.plist as a launchd daemon:

launchctl load ~/Library/LaunchAgents/LocationChanger.plist

Logfile

Logfile location can be adjusted in locationchanger, around line 12:

exec &>/usr/local/var/log/locationchanger.log

See log in action:

tail -f /usr/local/var/log/locationchanger.log

Run arbitrary script when location changes

By convention, placing an executable script in this directory with name:

locationchanger.callout.sh

and then running the installer, will cause the locationchanger service to run that script each time location changes.

Testing

For ease in testing, configure two locations within the current environment, e.g., "home" and "guest", each associated with a different SSID, such as the main SSID and guest SSID on your router. Then using the Wi-Fi menu, toggle between those SSIDs. You can see any success or error messages that are written to the log with a command like:

tail /usr/local/var/log/locationchanger.log

MacOS Ventura fallback to Automatic location issue

This repository already handles the case already if the SSID is not listed in the config file. In that case it should switch to the location Automatic, but it requires root access to do so. If you want that it changes the settings without the need to execute it again for each new network with root credentials just add to following to settings and install it again with ./install.sh:

Add <string>/usr/bin/sudo</string> within the array config

# LocationChanger.plist
...
<array>
    <string>/usr/bin/sudo</string>
    <string>/usr/local/bin/locationchanger</string>
</array>
...

Modify the sudoers file

  • Run sudo visudo
  • replace your_user_name with your username
    • you can type whoami to see you current username
# User privilege specification
root    ALL=(ALL) ALL
%admin  ALL=(ALL) ALL
your_user_name ALL=(ALL) NOPASSWD: /usr/local/bin/locationchanger

Run the ./install.sh script again - and now your location network settings are set to Automatic if no mapping has been found!

wifi-location-changer's People

Contributors

abencz avatar ericlifeac avatar fudesign2008 avatar infn8 avatar jikamens avatar kaylukas avatar klizzy avatar lrangelaugusto avatar luozhihua avatar marcool04 avatar mihaisiia avatar mikedevita avatar mr-david avatar putchi avatar red54 avatar rimar avatar rocco avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wifi-location-changer's Issues

Manual changes get overriden

When changing something in the network settings and location changer is running the changes get rolled back to the previous values.
As a workaroung it is possible to do
launchctl unload ~/Library/LaunchAgents/LocationChanger.plist
make the change
launchctl load ~/Library/LaunchAgents/LocationChanger.plist

A proper fix should be checking in the script that what changed is the SSID and only then doing scselect. Probably requires writing last SSID to file.

Default settings

If you connect to a WiFi that has for example a manual IP setting and then try to connect to a WiFi that you don't have any settings for, wifi-location-changer wouldn't change the previous settings which is totally expected but may not be what you expect.
I wish I could create a default location name and when there is no configurations available for a WiFi, wifi-location-changer would use that default settings.

Support for macOS Monterey (12.0)

Good morning!

Just reaching out to see if there's any plan to fully support macOS Monterey potentially on the horizon? I'd love to be able to use this tool actively on Monterey.

Thanks!

support for sonoma 14

Apple M1 Pro
macOS Sonoma 14.4 Beta
image
image
image

conf in sonoma

cat /usr/local/bin/locationchanger.conf
# This is a sample file for creating text file "locationchanger.conf" which associates location names with wifi names.
#
# Below, enter two pieces of text: location and wifi, separated by space(s)
# If the wifi name contains space(s), make sure to put quotes around that wifi name; location may NOT have any spaces or odd characters.
# Any line with a pound sign in front will be ignored as a comment. Blank lines are also ignored.
#
#
# LOCATION  WIFI
#
# For example:
# Home myHomeWifiSSID
# Work "my Work Wifi"
TPARTS-GFW "TPARTS-GFW"

Detect hostname and MAC address of router

Thanks for publishing this nice little utility. I was thinking it would be a useful new feature to be able to detect the hostname and MAC address of the current router, and use this this to switch location. SSID is good enough in many cases, of course, but naturally not all.

The wifi change cannot be detected on the latest Mac OS big sur system

It has been working well before upgrading to MacOS big sur. After the upgrade, the WIFI change cannot be detected.

If I manually run the command /usr/local/bin/locationchanger, the location can be switched correctly. This positive script can work normally. I think it just can't detect the change of wifi.

I guess it is caused by one of the following two reasons:

  1. The WatchPaths of $HOME/Library/LaunchAgents/LocationChanger.plist is invalid;
<key>WatchPaths</key>
<array>
  <string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string>
</array>
  1. After switching WiFI, /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist is indeed not changed.

Default location for unknown SSIDs.

I would like to use a location "Home" when I'm at home, but location "Automatic" when I'm anywhere else.
It wasn't clear from the README file, but I gather "Automatic" is already the default location if none of the listed SSIDs match the one that is currently connected to. Still, it would be nice to allow setting a default location. So maybe allow:

MyDefaultLocation *

to explicitly mean use location "MyDefaultLocation" for any WiFiSSID that is not explicitly listed. Thank you for a very nice and very needed utility!

external configuration: remove requirement for modification of source

Currently, users are expected to change the source code to add configuration about their locations and wifi names. This means merging when source updates happen.

Instead, introduce a feature for configuration to be stored in a text file that is external to project source (identified in a .gitignore entry).

Install error

After starting the instalation with the 'install' script and after input of the password I got the following error:

/Users//Library/LaunchAgents/LocationChanger.plist: Could not find specified service

What can be the cause? I am using OSX High Sierra 10.13.4.

mavericks

not sure why yet, but it seems to be broken on 10.9 . will send a pull request if i find the cause.

Ventura release of macOS may remove "network locations" UI feature

This article implies that the upcoming Ventura release of macOS will remove "network locations" as a UI feature of MacOS. I'm not sure what bearing that might have on the wifi-location-changer tool, but it could be significant.

Recently, I came across a tool that seems to offer "wifi change listening" functionality without reference to network location.

Triggering too often

I changed my plist file WatchPaths to the individual file preferences.plist in /Library/Preferences/SystemConfiguration which gets changed every time I change wifi network or change Location, thus triggering the script only as necessary. Using the whole folder caused the script to run every 10 seconds on my machine. Check the console logs on your machine to see how often the script is being called. Look for error message "Throttling respawn: will restart in n seconds"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.