Skip to content

Instantly share code, notes, and snippets.

@r10r
Last active December 22, 2015 16:28
Show Gist options
  • Save r10r/6499368 to your computer and use it in GitHub Desktop.
Save r10r/6499368 to your computer and use it in GitHub Desktop.
Commandline network selection script for OS X.
tree ~/.network/
~/.network/
├── down.d
│   └── mynet_down.sh
└── up.d
└── mynet_up.sh
#!/bin/bash
echo "Route DMZ internally 195.145.166.0/27"
route add -net 66.66.66.66 192.168.1.1 255.255.255.224
#!/bin/bash
echo "Remove Route"
route delete -net 66.66.66.66 192.168.1.1 255.255.255.224
#!/bin/sh
# TODO add bash_completion for profile names, is better than error handling ?
show_help() {
echo "-> Profile name must start with or equal pattern (case-inensitive)."
echo "-------------------------------------------------------------------"
scselect
exit 1
}
error() {
echo "ERROR: $1" >&2
show_help
}
# -- select profile --
[ -n "$1" ] || error "No pattern given for selection!"
profiles=`scselect | tail -n +2`
current=`echo "$profiles" | grep '^ \*' | cut -f 2 | tr -d \(\)`
new=`echo "$profiles" | grep -i -o "($1)" | tr -d \(\)`
# select by start-with if there is no exact match
if [ -z "$new" ]; then
new=`echo "$profiles" | grep -i -o "($1.*)" | tr -d \(\)`
count=`echo "$new" | wc -l | tr -d ' '`
[ -n "$new" ] || error "No profile name starting with '$1'"
[ $count -eq 1 ] || error "Multiple profiles(#$count) start with '$1'."
fi
[ "$current" != "$new" ] || error "Profile '$1' already selected. Pick a new one."
# -- switch profile --
echo "Switching network profile: ${current} -> ${new}"
sudo -s <<EOF
[ -f $HOME/.network/down.d/$current ] && . $HOME/.network/down.d/$current
scselect ${new}
[ -f $HOME/.network/up.d/$new ] && sleep 3 && . $HOME/.network/up.d/$new
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment