Skip to content

Instantly share code, notes, and snippets.

@cmavr8
Last active August 29, 2015 13:58
Show Gist options
  • Save cmavr8/10293082 to your computer and use it in GitHub Desktop.
Save cmavr8/10293082 to your computer and use it in GitHub Desktop.
TU/e MetaForum WiFi enabler/disabler
#!/bin/bash
# TU/e MetaForum WiFi enabler/disabler
# By Chris Mavrakis
#
# Usage:
# sudo mfwifi [enable|disable|setup]
#
# setup should be run only once, the first time. It asks for TU/e credentials,
# does not send them back to me, and creates the appropriate files in /etc/network
#
# enable stops network-manager, kills the dhcp client tha may be running, and enables
# the custom TU/e network configuration
#
# disable does the opposite of enable. Bring everything back to normal so you can connect
# to "normal" networks outside TU/e
#
#
# If no option is given, user will be queried
#
# I know it could be cleaner, and some of the killalls may be unnecessary,
# and that more checks should be done but that's all the time I want to
# spend right now. If you want to improve it let me know, we can setup a
# repo and you can commit changes.
export PATH=$PATH:/bin:/usr/bin:/usr/local/bin
# Log file to use:
log=/tmp/metaforum_wifi_switcher.log
# Stage 1 - Parse input, or ask for some if none was given
if [[ $1 == "enable" ]]; then
sel=1
elif [[ $1 == "disable" ]]; then
sel=2
elif [[ $1 == "setup" ]]; then
sel=3
else
echo -e "\nTU/e MetaForum WiFi enabler/disabler\n"
echo "No option provided. What do you want to do?"
echo "1. Enable MF wifi (for when you are at TU/e)"
echo "2. Disable MF wifi (for when you go home)"
echo "3. Setup (for the first time)"
echo -e "4. Exit\n"
while true; do
read -p "Selection? " response
case $response in
[1]* ) sel=1; break;;
[2]* ) sel=2; break;;
[3]* ) sel=3; break;;
[4]* ) exit;;
* ) echo "Please just give a number.";;
esac
done
fi
# Part 2 - Decide what to do, depending on input that we should now have
if [[ $sel == 1 ]]; then
# User selected the enable option
echo "Will now Enable MF wifi. Please wait..."
echo "*** "`date`": Starting MF wifi enabling function" &>> $log
service network-manager stop &>> $log
killall dhclient &>> $log
cp /etc/network/interfaces.manual /etc/network/interfaces
ifdown wlan0 &>> $log
ifup wlan0 &>> $log
echo "Done! Will now check for internet connection..."
echo "*** Enabling done" &>> $log
pkt=`ping 4.2.2.2 -c5 |grep "packet loss" | awk '{print $4}'`
if [[ $pkt == 5 ]]; then
echo "Excellent internet connection! 5/5"
elif [[ $pkt > 2 ]]; then
echo "Internet connection OK, $pkt/5"
elif [[ $pkt > 0 ]]; then
echo "Bad internet connection, $pkt/5"
else
echo "No internet connection"
fi
elif [[ $sel == 2 ]]; then
# User selected the disable option
echo "Will now Disable MF wifi."
echo "*** "`date`": Starting MF wifi disabling function" &>> $log
ifdown wlan0 &>> $log
killall dhclient &>> $log
cp /etc/network/interfaces.default /etc/network/interfaces
service network-manager start &>> $log
echo "Done!"
echo "*** Disabling done" &>> $log
elif [[ $sel == 3 ]]; then
# User selected the setup option
while true; do
read -p "Are you sure you want to do initial setup? [y/n] " yn
case $yn in
[Yy]* )
cp /etc/network/interfaces /etc/network/interfaces.default # backup original file
cp /etc/network/interfaces /etc/network/interfaces.manual # make a copy for us to work with, too
read -p "Your TU/e username? " usr
read -s -p "Your TU/e password? (I won't tell anyone!) " psw
# add the magic ingredients to the interfaces file
echo "# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
auto wlan0
iface wlan0 inet dhcp
wireless-mode Managed
wpa-ssid tue-wpa2
wpa-ap-scan 1
wpa-proto RSN WPA
wpa-pairwise CCMP AES
wpa-group CCMP AES
wpa-key-mgmt WPA-EAP
wpa-eap PEAP
wpa-identity $usr
wpa-password $psw
wpa-phase1 fast_provisioning=1" > /etc/network/interfaces.manual
echo -e "\nInstallation completed! Re-run script to enable MF wifi."
break;;
[Nn]* )
echo "Aborted setup. Exiting..."
exit;;
* ) echo "Please answer yes or no.";;
esac
done
else
echo "Why the hell are we here? Generic error, system malfunction, blue screen..."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment