Last active
August 12, 2017 17:06
-
-
Save SwagDevOps/eedd97f7c1d6b79c6af70c50c1c509f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env sh | |
| # | |
| # Use this script in a crontab to execute specific scripts when | |
| # your machine is connected to a specific network | |
| # scripts are stored in ``/etc/network/scripts/${NETWORK_NAME}`` | |
| # | |
| # Also ensure to restart failed miredo (if present) | |
| # | |
| # Using "Ubuntu 16.10" with "wicd 1.7.4 (bzr-r961)" | |
| # scripts as ``postconnect`` are not properly executed, even when | |
| # defined in ``/etc/wicd/wire*settings.conf`` | |
| set_status() { | |
| export NETWORK_TYPE NETWORK_STATUS NETWORK_NAME | |
| status=$(LANGUAGE=en_US; wicd-cli --status) | |
| NETWORK_TYPE=$(echo "${status}" | egrep '^Connection type:\s{1}' | awk -F ': ' '{print $2}') | |
| NETWORK_STATUS=$(echo "${status}" | head -1 | awk -F ': ' '{print $2}') | |
| NETWORK_NAME=$(echo "${status}" | egrep '^Connected to\s{1}' | awk '{print $3}') | |
| } | |
| miredo_check() { | |
| which miredo > /dev/null || return 0 | |
| systemctl status miredo | grep -vqi failed | |
| } | |
| systemctl() { | |
| /usr/bin/env systemctl --no-ask-password --system "$@" | |
| } | |
| execute_network_script() { | |
| network_script="/etc/network/scripts/${NETWORK_NAME}" | |
| test -n "${NETWORK_NAME}" || return 0 | |
| test -f "${network_script}" || return 0 | |
| test -x "${network_script}" || return 0 | |
| "${network_script}" | |
| } | |
| main() { | |
| set_status | |
| echo "${NETWORK_STATUS}" | egrep -q '^Connected' || { | |
| echo 'Not connected' 1>&2 | |
| return 100 # ENETDOWN 100 Network is down | |
| } | |
| echo "${NETWORK_STATUS} to '${NETWORK_NAME:-unnamed}' (${NETWORK_TYPE})" | |
| miredo_check || systemctl restart miredo | |
| execute_network_script | |
| } | |
| main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment