#!/bin/bash # Company: PowerCraft Technology # Author: Copyright Jelle de Jong # Note: Please send me an email if you enhanced the script # Version: 0.0.2 # Date: 2009-01-07 / 2009-01-23 # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more de/usr/bin/tails. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. # testing: # any [1x] # ieee8021x_eap_ttls [1x] # roaming [0x] # wep [0x] # wpa-personal [2x] version="0.0.2" author="Jelle de Jong " donation="http://www.tuxcrafter.net/pages/contact.html#paypal-donation" ANY=0 IEEE8021X_EAP_TTLS=0 ROAMING=0 WEP=0 WPA_PERSONAL=0 unset SSID unset PASSWORD unset INDENTITY unset DEVICE unset CONFIG network_manager=0 scanning=0 reset=0 SYSCONF="/etc/pct-wireless-setup.conf" SYSDIR="/etc/pct-wireless-setup.conf.d/" function version() { echo "Usage: $0 --help" echo "Version: $version" echo "Author: $author" echo "Donation: $donation" } function usage() { echo "configure wireless networks with system width configuration and profiles" echo echo "Usage: $0 [options]" echo echo "Options:" echo " -h, --help, -? show this help information" echo " -a, --any connect to the first open ssid available" echo " -x, --ieee8021x-eap-ttls connect to special network used in some universities" echo " -r, --roaming connect to open networks when found, for traveling" echo " -e, --wep connect to wep secured network" echo " -w, --wpa-personal connect to wpa-psk secured network" echo " -s, --ssid ... provide ssid as option, see examples" echo " -p, --password ... provide password as option, see examples" echo " -i, --identity ... provide identity, used with --ieee8021x-eap-ttls" echo " -d, --device ... provide network device name" echo " -c, --config ... name of id-string used in configuration file" echo " -n, --network-manager configure system for network-manager to take control" echo " -t, --reset removes 70-persistent-net.rules file (reboot needed)" echo " -g, --scanning scan for wireless networks" echo " -v, --version show the version and author information" echo echo "Example:" echo " pct-wireless-setup --ieee8021x-eap-ttls --ssid HHS-802-1x --identity 21031191 --password pass444" echo " pct-wireless-setup -x -s HHS-802-1x -i 21031191 -p pass444 -d wlan0" echo " pct-wireless-setup --any --device wlan0" echo " pct-wireless-setup --roaming --device wlan0" echo " pct-wireless-setup --wep --ssid meso --password 8FC31B04E051676573F69A45FF" echo " pct-wireless-setup --wpa-personal --ssid linux-linksys --password pass555" echo " pct-wireless-setup -w -s linux-linksys -p pass555" echo " pct-wireless-setup --network-manager" echo " pct-wireless-setup --reset" echo " pct-wireless-setup --scanning" echo " pct-wireless-setup --version" echo echo " pct-wireless-setup --wpa-personal --config config0" echo " pct-wireless-setup --wep --config location" echo echo " pct-wireless-setup -config location0" echo " pct-wireless-setup -config config0" echo echo " ifconfig -a" echo echo "Configuration:" echo " $SYSCONF" echo " $SYSDIR" echo echo " In the configuration files the ssid, password, identity and device" echo " can be set, so only options as --wep or --wpa-personal are needed" echo " to configure the wireless network. Look inside the files to get" echo " a better idea of how to configure them. The --config option can" echo " be used to select additional configurations. For example using" echo " --wep --config location0 will use the section [wep-location0] and" echo " using --config config0 will use the section [config0]." echo echo " This program is a helper script to easy and fast setup common" echo " wireless network configurations. You can debug this script by" echo " running it as bash -x pct-wireless-setup [options]. The script" echo " is just a setup wrapper for the /etc/network/interfaces and the" echo " /etc/wpa_supplicant.conf configuration files." echo echo "Description: [package]" echo " This package contains a script to setup wireless networking environments." echo " The program can be used to configure the /etc/network/interfaces and" echo " /etc/wpa_supplicant files with a system width settings. The program should" echo " makes it easy to setup wep, wpa, wpa-personal, roaming wireless networks" echo " and ieee8021x-eap-ttls used by some universities. The program has a" echo " configuration system where different setups can be defined, this makes it" echo " easy to switching between multiple networks. The script is designed to" echo " efficiently setup and maintain network configurations for wireless clients." echo " Take a look at the help argument to see all options, help, and useful" echo " examples. The package is maintained by PowerCraft Technology." echo echo "Website:" echo " https://secure.powercraft.nl/svn/packages/" echo "Mailinglist:" echo " http://www.powercraft.nl/cgi-bin/mailman/listinfo/packages-commit" echo " http://www.powercraft.nl/cgi-bin/mailman/listinfo/packages-devel" } while [[ "$1" == -* ]]; do case "$1" in -h|--help|-\?) usage; exit 0;; -a|--any) ANY=1; shift;; -x|--ieee8021x-eap-ttls) IEEE8021X_EAP_TTLS=1; shift;; -r|--roaming) ROAMING=1; shift;; -e|--wep) WEP=1; shift;; -w|--wpa-personal) WPA_PERSONAL=1; shift;; -s|--ssid) SSID="$2"; shift; shift;; -p|--password) PASSWORD="$2"; shift; shift;; -i|--identity) INDENTITY="$2"; shift; shift;; -d|--device) DEVICE="$2"; shift; shift;; -c|--config) CONFIG="$2"; shift; shift;; -n|--network-manager) network_manager=1; shift;; -t|--reset) reset=1; shift;; -g|--scanning) scanning=1; shift;; -v|--version) version; exit 0;; --) shift; break;; -*) echo "invalid option: $1"; usage; exit 1;; esac done if [ $(id -u) != 0 ]; then echo "please run this script with superuser permissions" echo "example: sudo $0 [options]" exit 1 fi function scan_configuration() { file="$1" unset options [ ! -z "$SSID" ] && options+=("SSID") [ ! -z "$PASSWORD" ] && options+=("PASSWORD") [ ! -z "$INDENTITY" ] && options+=("INDENTITY") [ ! -z "$DEVICE" ] && options+=("DEVICE") [ ! -z "$CONFIG" ] && options+=("CONFIG") for value in "${options[@]}" do sed -n -i -e "/$value/!p" "$file" done } function get_configuration() { section="$1" if [ ! -z "$section" ] && [ ! -z "$CONFIG" ]; then section+="-$CONFIG" elif [ -z "$section" ] && [ ! -z "$CONFIG" ]; then section="$CONFIG" fi [ ! -d "$SYSDIR" ] && mkdir --parents --verbose "$SYSDIR" tempfile=$(mktemp -p "/tmp/" tempfile.XXXXXXXXXX) trap "rm -f $tempfile" 0 1 2 3 15 [ -z "$tempfile" ] && exit 1 files="$SYSCONF" files+=$'\n' files+=$(run-parts --list "$SYSDIR" ) if [ -n "$files" ]; then while read file; do set +e [ ! -r "$file" ] && continue sed -n '/^\['"$section"'\]$/,/^\[.*\]$/{//!p}' "$file" > "$tempfile" scan_configuration "$tempfile" . "$tempfile" set -e done < <(echo "$files") fi } function any() { echo "pct-wireless-setup: any: configure interfaces file" get_configuration any if [ -z $DEVICE ]; then echo "device not set, try adding --device wlan0, closing now" exit 1 fi /etc/init.d/networking stop [ -e /etc/network/interfaces ] && mv --verbose /etc/network/interfaces /etc/network/interfaces.backup touch /etc/network/interfaces echo auto lo | tee -a /etc/network/interfaces echo iface lo inet loopback | tee -a /etc/network/interfaces echo | tee -a /etc/network/interfaces echo auto $DEVICE | tee -a /etc/network/interfaces echo iface $DEVICE inet dhcp | tee -a /etc/network/interfaces echo pre-up /sbin/ifconfig $DEVICE up | tee -a /etc/network/interfaces echo pre-up /sbin/iwconfig $DEVICE mode managed | tee -a /etc/network/interfaces echo pre-up /sbin/iwconfig $DEVICE essid any | tee -a /etc/network/interfaces echo | tee -a /etc/network/interfaces /etc/init.d/networking start } function ieee8021x_eap_ttls() { echo "pct-wireless-setup: ieee8021x-eap-ttls: configure interfaces file" get_configuration ieee8021x-eap-ttls if [ -z "$SSID" ]; then echo "ssid not set, try adding --ssid something, closing now" exit 1 fi if [ -z "$INDENTITY" ]; then echo "identity not set, try adding --identity something, closing now" exit 1 fi if [ -z "$PASSWORD" ]; then echo "password not set, try adding --password something, closing now" exit 1 fi if [ -z $DEVICE ]; then echo "device not set, try adding --device wlan0, closing now" exit 1 fi /etc/init.d/networking stop [ -e /etc/network/interfaces ] && mv --verbose /etc/network/interfaces /etc/network/interfaces.backup touch /etc/network/interfaces echo auto lo | tee -a /etc/network/interfaces echo iface lo inet loopback | tee -a /etc/network/interfaces echo | tee -a /etc/network/interfaces echo auto $DEVICE | tee -a /etc/network/interfaces echo iface $DEVICE inet dhcp | tee -a /etc/network/interfaces echo pre-up /sbin/wpa_supplicant -B -i $DEVICE -c /etc/wpa_supplicant.conf | tee -a /etc/network/interfaces echo post-down killall -q wpa_supplicant | tee -a /etc/network/interfaces echo | tee -a /etc/network/interfaces echo "ctrl_interface=/var/run/wpa_supplicant eapol_version=1 network={ ssid=\"$SSID\" scan_ssid=1 key_mgmt=IEEE8021X eap=TTLS phase2=\"auth=PAP\" identity=\"$INDENTITY\" password=\"$PASSWORD\" priority=3 }" > /etc/wpa_supplicant.conf /etc/init.d/networking start } function roaming() { echo "pct-wireless-setup: roaming: configure interfaces file" get_configuration roaming if [ -z $DEVICE ]; then echo "device not set, try adding --device wlan0, closing now" exit 1 fi /etc/init.d/networking stop [ -e /etc/network/interfaces ] && mv --verbose /etc/network/interfaces /etc/network/interfaces.backup touch /etc/network/interfaces echo auto lo | tee -a /etc/network/interfaces echo iface lo inet loopback | tee -a /etc/network/interfaces echo | tee -a /etc/network/interfaces echo allow-hotplug $DEVICE | tee -a /etc/network/interfaces echo iface $DEVICE inet manual | tee -a /etc/network/interfaces echo wpa-driver wext | tee -a /etc/network/interfaces echo wpa-roam /etc/wpa_supplicant.conf | tee -a /etc/network/interfaces echo iface default inet dhcp | tee -a /etc/network/interfaces echo | tee -a /etc/network/interfaces echo 'network={ key_mgmt=NONE }' > /etc/wpa_supplicant.conf /etc/init.d/networking start } function wep() { echo "pct-wireless-setup: wep: configure interfaces file" get_configuration wep # information: # wlanconfig wlan0 list scan #~ /etc/init.d/networking stop #~ ifconfig wlan0 down #~ ifconfig wifi0 down #~ iwconfig wlan0 ap 00:16:B6:E7:0C:33 #~ iwconfig wlan0 key 8FC31B04E051676573F69A45FF #~ ifconfig wlan0 up #~ dhclient wlan0 #~ iwconfig wlan0 essid "osem" #~ iwconfig wlan0 key "s:osemosem" #~ dhclient wlan0 #~ echo wireless-essid osem | tee -a /etc/network/interfaces #~ echo wireless-key 8FC31B04E051676573F69A45FF | tee -a /etc/network/interfaces if [ -z "$SSID" ]; then echo "ssid not set, try adding --ssid something, closing now" exit 1 fi if [ -z "$PASSWORD" ]; then echo "password not set, try adding --password something, closing now" exit 1 fi if [ -z $DEVICE ]; then echo "device not set, try adding --device wlan0, closing now" exit 1 fi /etc/init.d/networking stop [ -e /etc/network/interfaces ] && mv --verbose /etc/network/interfaces /etc/network/interfaces.backup touch /etc/network/interfaces echo auto lo | tee -a /etc/network/interfaces echo iface lo inet loopback | tee -a /etc/network/interfaces echo | tee -a /etc/network/interfaces echo auto $DEVICE | tee -a /etc/network/interfaces echo iface $DEVICE inet dhcp | tee -a /etc/network/interfaces echo pre-up /sbin/wpa_supplicant -B -i $DEVICE -c /etc/wpa_supplicant.conf | tee -a /etc/network/interfaces echo post-down killall -q wpa_supplicant | tee -a /etc/network/interfaces echo | tee -a /etc/network/interfaces # ap_scan=1 # fast_reauth=1 # eapol_version=1 echo "network={ ssid=\"$SSID\" key_mgmt=NONE wep_key0=$PASSWORD }" | tee /etc/wpa_supplicant.conf /etc/init.d/networking start } function wpa_personal() { echo "pct-wireless-setup: wpa-personal: configure interfaces file" get_configuration wpa-personal if [ -z "$SSID" ]; then echo "ssid not set, try adding --ssid something, closing now" exit 1 fi if [ -z "$PASSWORD" ]; then echo "password not set, try adding --password something, closing now" exit 1 fi if [ -z $DEVICE ]; then echo "device not set, try adding --device wlan0, closing now" exit 1 fi /etc/init.d/networking stop [ -e /etc/network/interfaces ] && mv --verbose /etc/network/interfaces /etc/network/interfaces.backup touch /etc/network/interfaces echo auto lo | tee -a /etc/network/interfaces echo iface lo inet loopback | tee -a /etc/network/interfaces echo | tee -a /etc/network/interfaces echo auto $DEVICE | tee -a /etc/network/interfaces echo iface $DEVICE inet dhcp | tee -a /etc/network/interfaces echo pre-up /sbin/wpa_supplicant -B -i $DEVICE -c /etc/wpa_supplicant.conf | tee -a /etc/network/interfaces echo post-down killall -q wpa_supplicant | tee -a /etc/network/interfaces echo | tee -a /etc/network/interfaces echo "network={ ssid=\"$SSID\" key_mgmt=WPA-PSK psk=\"$PASSWORD\" }" > /etc/wpa_supplicant.conf /etc/init.d/networking start } function network_manager() { echo "pct-wireless-setup: network-manager: configure interfaces file" /etc/init.d/networking stop [ -e /etc/network/interfaces ] && mv --verbose /etc/network/interfaces /etc/network/interfaces.backup touch /etc/network/interfaces echo | tee -a /etc/network/interfaces echo auto lo | tee -a /etc/network/interfaces echo iface lo inet loopback | tee -a /etc/network/interfaces echo | tee -a /etc/network/interfaces rm --verbose /etc/wpa_supplicant.conf /etc/init.d/networking start } function scanning() { echo "pct-wireless-setup: scanning: iwlist scanning" if [ -z $DEVICE ]; then echo "device not set, try adding --device wlan0, closing now" exit 1 fi ifconfig $DEVICE up iwlist scanning } function reset() { echo "pct-wireless-setup: reset: remove 70-persistent-net.rules" file="/etc/udev/rules.d/70-persistent-net.rules" rm --verbose --interactive "$file" [ -e "$file" ] || echo "please reboot your system, or replug your network devices" } if [ ! -z $CONFIG ]; then get_configuration; fi if [ $ANY = 1 ]; then any; elif [ $IEEE8021X_EAP_TTLS = 1 ]; then ieee8021x_eap_ttls; elif [ $ROAMING = 1 ]; then roaming; elif [ $WEP = 1 ]; then wep; elif [ $WPA_PERSONAL = 1 ]; then wpa_personal; elif [ $network_manager = 1 ]; then network_manager; elif [ $scanning = 1 ]; then scanning; elif [ $reset = 1 ]; then reset; else echo "pct-wireless-setup: configuration variables not found, exiting now" fi exit