Last active
December 22, 2015 16:28
-
-
Save r10r/6499368 to your computer and use it in GitHub Desktop.
Commandline network selection script for OS X.
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
tree ~/.network/ | |
~/.network/ | |
├── down.d | |
│ └── mynet_down.sh | |
└── up.d | |
└── mynet_up.sh |
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
#!/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 |
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
#!/bin/bash | |
echo "Remove Route" | |
route delete -net 66.66.66.66 192.168.1.1 255.255.255.224 |
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
#!/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