Last active
December 29, 2015 13:29
-
-
Save dessibelle/7678063 to your computer and use it in GitHub Desktop.
Connects AFP shares (or any URL/path compatible with open really) depending on currently connected wifi network. Ideally executed using `cron` or `launchd`
This file contains 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 bash | |
PROGRAM_NAME=`basename $0` | |
DEBUG=false | |
TARGET_ROUTER_MAC=() | |
SHARE=() | |
function debug { | |
[ $DEBUG == true ] && echo "$1" | |
} | |
ARGS=`getopt t:s:dh "$@"` | |
if [ $? != 0 ] || [ $# -lt 1 ] ; then | |
echo "Error parsing arguments. Try $PROGRAM_NAME -h" > /dev/stderr | |
exit 2 | |
fi | |
eval set -- "$ARGS" | |
while true; do | |
case $1 in | |
-t) | |
TARGET_ROUTER_MAC+=("$2"); shift 2; continue | |
;; | |
-s) | |
SHARE+=("$2"); shift 2; continue | |
;; | |
-d) | |
DEBUG=true; shift; continue | |
;; | |
-h) | |
BA="\033[1m" | |
BD="\033[0m" | |
UA=`tput smul` | |
UD=`tput rmul` | |
echo "$PROGRAM_NAME" | |
echo "" | |
echo -e "${BA}USAGE${BD}" | |
echo -e " $PROGRAM_NAME [${BA}-dh${BD}] ${BA}-t${BD} ${UA}target_mac${UD} ${BA}-s${BD} ${UA}share_url${UD}" | |
echo "" | |
echo -e "${BA}GENERAL${BD}" | |
echo -e " Connects AFP shares (or any URL/path compatible with ${BA}open${BD} really) depending on currently connected wifi network." | |
echo "" | |
echo -e "${BA}OPTIONS${BD}" | |
echo -e " ${BA}-t${BD} ${UA}target_mac${UD}" | |
echo " MAC address of the target router(s)" | |
echo "" | |
echo -e " ${BA}-s${BD} ${UA}share_url${UD}" | |
echo " URL of share(s) to connect for the given target MAC address(es), | |
on the form afp://[user[:password]@]host/share_name" | |
echo "" | |
echo -e " ${BA}-h${BD} Display help" | |
echo "" | |
echo -e " ${BA}-d${BD} Display debugging messages" | |
echo "" | |
exit 0 | |
;; | |
--) | |
# no more arguments to parse | |
shift | |
break | |
;; | |
*) | |
printf "Unknown option %s\n" "$1" | |
exit 1 | |
;; | |
esac | |
done | |
ROUTER_IP=`netstat -rnf inet | grep default | awk {'print$2'}` | |
ROUTER_MAC=`netstat -rnf inet | grep -v link | grep -w ^$ROUTER_IP | awk {'print$2'}` | |
debug "Connected to router with MAC address $ROUTER_MAC" | |
debug "Will connect shares: $(IFS=", " ; echo "${SHARE[*]}")" | |
debug "If router MAC is: $(IFS=", " ; echo "${TARGET_ROUTER_MAC[*]}")" | |
if [ "$ROUTER_MAC" == "" ]; then | |
debug "Not connected, exiting." | |
# un_mount | |
exit 0 | |
fi | |
for TARGET_MAC in "${TARGET_ROUTER_MAC[@]}" ; do | |
debug "Trying MAC $TARGET_MAC" | |
if ! [ $TARGET_MAC == $ROUTER_MAC ]; then | |
debug " Not at target wifi" | |
# un_mount | |
fi | |
if [ $TARGET_MAC == $ROUTER_MAC ]; then | |
debug " Connected to target wifi" | |
for S in "${SHARE[@]}" ; do | |
debug " Connecting to share $S" | |
DIR=`basename "$S"` | |
if ! [ -d "/Volumes/$DIR" ] ; then | |
open "$S" | |
fi | |
done | |
fi | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment