Skip to content

Instantly share code, notes, and snippets.

@leggiero
Last active August 26, 2023 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leggiero/c8e496d6ca62a1ff096ee956cbd58e6c to your computer and use it in GitHub Desktop.
Save leggiero/c8e496d6ca62a1ff096ee956cbd58e6c to your computer and use it in GitHub Desktop.
Create persistent static ARP entries read from /etc/ethers when connected to specific Wi-Fi connection.
#!/usr/bin/bash
# The MIT License (MIT)
# Copyright 2023 Eduardo Leggiero
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
set -e
log_debug=0
log_info=0
notification=0
home_ssid=("My-Home-Wi-Fi")
ethers_file="/etc/ethers"
notify_message="Arp entries created for home network"
notification_user_id="1000"
main() {
journal_identifier="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
[[ "$journal_identifier" =~ ^[0-9]+-(.+)$ ]] && journal_identifier="${BASH_REMATCH[1]}"
if [[ "$log_debug" == "1" ]]; then
message="NetworkManager event: Action: $NM_DISPATCHER_ACTION"
[[ ! -z "$CONNECTIVITY_STATE" ]] && message="$message, State: $CONNECTIVITY_STATE"
[[ ! -z "$CONNECTION_ID" ]] && message="$message, Id: $CONNECTION_ID"
debug "$message"
fi
if [[ "$NM_DISPATCHER_ACTION" == "up" ]]; then
ssid="$(nmcli -t -f active,ssid dev wifi | grep -E '^yes' | cut -d: -f2)"
if [[ "$ssid" == "$home_ssid" ]]; then
[[ "$log_info" == "1" ]] && info "Creating arp entries for home network \"$ssid\" Wi-Fi connection"
arp -f "$ethers_file"
[[ "$notification" == "1" ]] && sudo -u "#$notification_user_id" \
DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/notification_user_id/bus" \
notify-send --transient --app-name "$journal_identifier" "$notify_message" || true
elif [[ "$log_debug" == "1" ]]; then
debug "Ignored as it's not the home network connection: \"$CONNECTION_ID\""
fi
#
fi
}
log() {
echo "$1" | systemd-cat -t "$journal_identifier" -p "$2"
}
info() {
log "$1" info
}
debug() {
log "$1" debug
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment