Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- [ -z "$1" -o -z "$2" ] && echo "w_iface up/last/down" && exit 0
- rm .list.tmp 2>/dev/null
- wpa_config="/etc/wpa_supplicant/wpa_supplicant.conf"
- last_ssid=$(cat $wpa_config | grep ssid=\" | awk -F'=' '{print $2}')
- case $2 in
- down)
- pkill wpa_supplicant
- dhclient $1 -x
- ifconfig $1 down
- exit 0
- ;;
- last)
- pkill wpa_supplicant
- dhclient $1 -x
- ifconfig $1 down
- ifconfig $1 up
- if [[ -z `cat /etc/network/interfaces | grep $1` ]]; then
- wpa_supplicant -B -Dwext -i $1 -c "$wpa_config"
- dhclient $1
- else
- ifup $1
- fi
- exit 0
- ;;
- up)
- pkill wpa_supplicant
- dhclient $1 -x
- #dhclient -r
- ifconfig $1 up
- iwlist $1 scan 2>/dev/null > .list.tmp
- eval list=( $(cat .list.tmp | awk -F":" '/ESSID/{print $2}' | nl -w2 -n rz -s "=") )
- #sets prompt
- PS3="Choose wifi connection: "
- #tests for number of wifi connections, exits if none
- if [[ -z "${list[0]}" ]]; then
- clear
- echo "No available wifi connection"
- exit 1
- fi
- #menu of wifi connections
- select item in "${list[@]}"; do
- cell=$(echo $item | awk -F"=" '{print $1}')
- essid=$(echo $item | awk -F"=" '{print $2}')
- echo " "
- echo "$essid: Connecting..."
- echo " "
- sed -ni "/Cell $cell/,/Cell/ p" .list.tmp
- # delete spaces
- echo "$(cat .list.tmp | sed -n 's/^[ \t]*//')"
- #sets channel as value for CHANNEL variable
- channel=$(grep Channel: .list.tmp | sed 's/.*Channel://g')
- #sets pairwise chiphers
- pairwise=$(grep -m 1 Pairwise .list.tmp | sed 's/.* : //g')
- #sets group chiphers
- group=$(grep -m 1 Group .list.tmp | sed 's/.* : //g')
- #test for mode, if mode = master, sets MODE variable to managed
- mode=$(grep Mode .list.tmp | sed 's/.*Mode://g')
- if [[ "$mode" == "Master" ]]; then
- mode="managed"
- else
- clear
- echo "Cannot connect"
- exit 1
- fi
- #tests for encryption key
- key=$(grep key: .list.tmp | sed 's/.*key://g')
- if [[ $key == "on" ]]; then
- while true; do
- read -r -s -p "$essid: Enter password: " key
- if [ ${#key} -lt 8 ] || [ ${#key} -gt 32 ]; then
- echo "The password must be 8-32 characters"
- :
- else
- break
- fi
- done
- fi
- skey=$(wpa_passphrase "$essid" $key 2>/dev/null | sed -n 's/^[ \t]psk=//gp')
- #checks encryption algorithm
- IE=$(grep WPA .list.tmp | sed 's/.*: //g')
- if [[ -n "$IE" ]]; then
- echo "ctrl_interface=/var/run/wpa_supplicant
- ap_scan=1
- network={
- ssid=\"$essid\"
- scan_ssid=0
- proto=WPA RSN
- key_mgmt=WPA-PSK
- pairwise=$pairwise
- group=$group
- psk=$skey
- }" > "$wpa_config"
- if [[ -z `cat /etc/network/interfaces | grep $1` ]]; then
- sudo pkill wpa_supplicant
- sudo dhclient -r
- sudo ifconfig $1 up
- wpa_supplicant -B -Dwext -i $1 -c "$wpa_config" &>/dev/null
- dhclient $1
- else
- ifup $1
- fi
- exit 0
- else
- #sets the wireless configuration for non WPA: essid, channel, mode, key, etc
- sudo pkill wpa_supplicant
- sudo dhclient -r
- sudo ifconfig $1 up
- iwconfig $1 essid "$essid" channel $channel mode $mode key $key
- #connects to wifi connection
- dhclient $1
- exit 0
- fi
- done
- ;;
- *)
- exit 1
- esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement