https://github.com/arendruni/location-switch
Automatic network location switcher
https://github.com/arendruni/location-switch
launchagent launchd macos wifi
Last synced: about 1 year ago
JSON representation
Automatic network location switcher
- Host: GitHub
- URL: https://github.com/arendruni/location-switch
- Owner: arendruni
- License: mit
- Created: 2019-04-13T12:02:44.000Z (about 7 years ago)
- Default Branch: main
- Last Pushed: 2024-11-21T20:19:41.000Z (over 1 year ago)
- Last Synced: 2024-12-08T05:26:56.831Z (over 1 year ago)
- Topics: launchagent, launchd, macos, wifi
- Language: Shell
- Homepage:
- Size: 20.5 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Network Location Switch
This bash script is designed to change the network location on a Mac based on the matching MAC address or SSID of a known network location. This can be useful if you frequently switch between different networks with different settings.
The script works by comparing the MAC address or SSID of the currently connected network to a list of known MAC addresses or SSIDs. If there is a match, the script changes the network location to the corresponding predefined location. If there is no match, the script changes the network location default location.
For more information on creating and using network locations on macOS, you can refer to the official Apple support document: [Use network locations on Mac](https://support.apple.com/en-us/HT202480).
## Compatibility
This script is intended to be used on macOS 10.15 (Catalina) or later. It may not work on older versions of macOS.
Please ensure that your system meets the minimum requirements before attempting to run this script. If you encounter any issues, please let me know and I will do my best to assist you.
## Usage
To use this script, you will need to change the following variables at the beginning of the script:
- `DEFAULT_LOCATION` - the name of the default network location
- `KNOWN_LOCATIONS` - a list of known MAC addresses or SSIDs and their corresponding network locations
### `KNOWN_LOCATIONS` Format
The `KNOWN_LOCATIONS` array is a list of items that represent known locations in the format of MAC addresses and SSIDs. Each item in the array consists of three parts:
A prefix to indicate whether the item is a MAC address or an SSID.
The MAC address or SSID value.
A name for the location.
The prefix and value are separated by a colon (":") character. The name is separated from the value by another colon.
Note that the name for the location is required and must be provided for each item in the array.
Example:
```bash
KNOWN_LOCATIONS=(
"m:00:11:22:33:44:55:Home"
"s:MyWiFiSSID:Office"
)
```
#### MAC Address Format
MAC address items have the prefix "m". The value of a MAC address item is a standard MAC address string consisting of six colon-separated pairs of hexadecimal digits. The name of the location follows the MAC address value and is separated by a colon. Example: `m:00:11:22:33:44:55:Home`
#### SSID Format
SSID items have the prefix "s". The value of an SSID item is a string representing the name of a WiFi network. The name of the location follows the SSID value and is separated by a colon. Example: `s:MyWiFiSSID:Office`
## Demonizing the Script on macOS
To run this script as a background process on macOS, you can use the `launchd` system. Here's a template `launchd` configuration file you can use:
```xml
Label
io.giun.dev.location-switch
ProgramArguments
/path/to/location-switch.sh
-v
StartInterval
10
RunAtLoad
LowPriorityIO
StandardOutPath
/path/to/location-switch/output.log
StandardErrorPath
/path/to/location-switch/error.log
```
Edit the example file `io.giun.dev.location-switch.template.plist` to make sure to replace `/path/to/location-switch.sh` with the actual path to your script.
To load the `launchd` configuration, run the following command:
```shell
$ launchctl load io.giun.dev.location-switch.plist
```
This will start the script and run it every 10 seconds. You can view the script output in the log file specified in the configuration file. To stop the script, unload the `launchd` configuration with the following command:
```shell
$ launchctl unload io.giun.dev.location-switch.plist
```
Move the `.plist` file to the `~/Library/LaunchAgents` to launch the agent at startup.