Advertisement
constantin-net

wi-fi_bash

Oct 16th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.69 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #set -o nounset
  4. #set -o errexit
  5. rm .list.tmp 2>/dev/null
  6.  
  7. if [[ "$1" == "down" ]]; then  
  8.     pkill wpa_supplicant
  9.     ifconfig wlan0 down
  10.     exit
  11. fi
  12. if [[ "$1" == "reload" ]]; then
  13.     ifconfig wlan0 down
  14.     pkill wpa_supplicant
  15.     ifconfig wlan0 up
  16.     wpa_supplicant -B -Dwext -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf && dhclient wlan0
  17.     exit
  18. fi
  19.  
  20. ifconfig wlan0 down
  21. pkill wpa_supplicant
  22. ifconfig wlan0 up
  23.  
  24. iface="wlan0"
  25. iwlist $iface scan 2>/dev/null > .list.tmp
  26. eval list=( $(cat .list.tmp | awk -F":" '/ESSID/{print $2}' | nl -w2 -n rz -s "=") )
  27. #sets prompt
  28. PS3="Choose wifi connection: "
  29. #tests for number of wifi connections, exits if none
  30. if [[ -z "${list[0]}" ]]; then
  31.     clear
  32.     echo "No available wifi connection"
  33.     exit 1
  34. fi
  35. #menu of wifi connections
  36. select item in "${list[@]}"; do
  37.     #sets essid as value for WIFI variable and displays information about the AP
  38.     cell=$(echo $item | awk -F"=" '{print $1}')
  39.     essid=$(echo $item | awk -F"=" '{print $2}')
  40.     sed -ni "/Cell $cell/,/Cell/ p" .list.tmp
  41.     # delete spaces
  42.     echo "$(cat .list.tmp | sed -n 's/^[ \t]*//')"
  43.     #sets channel as value for CHANNEL variable
  44.     channel=$(grep Channel: .list.tmp | sed 's/.*Channel://g')
  45.     #sets pairwise chiphers
  46.     pairwise=$(grep -m 1 Pairwise .list.tmp | sed 's/.* : //g')
  47.     #sets group chiphers
  48.     group=$(grep -m 1 Group .list.tmp | sed 's/.* : //g')
  49.     #test for mode, if mode = master, sets MODE variable to managed
  50.     mode=$(grep Mode .list.tmp | sed 's/.*Mode://g')
  51.     if [[ "$mode" == "Master" ]]; then
  52.         mode="managed"
  53.     else
  54.         clear
  55.         echo "Cannot connect"
  56.         exit
  57.     fi
  58.     #tests for encryption key
  59.     key=$(grep key: .list.tmp | sed 's/.*key://g')
  60.     if [[ $key == "on" ]]; then
  61.         echo -n "Enter encryption key: "
  62.         read key
  63.     fi
  64.     skey=$(wpa_passphrase "$essid" $key 2>/dev/null | sed -n 's/^[ \t]psk=//gp')
  65.     #checks encryption algorithm
  66.     IE=$(grep WPA .list.tmp | sed 's/.*: //g')
  67.     if [[ -n "$IE" ]]; then
  68.         echo "ctrl_interface=/var/run/wpa_supplicant
  69. ap_scan=1
  70. network={
  71. ssid=\"$essid\"
  72. scan_ssid=0
  73. proto=WPA RSN
  74. key_mgmt=WPA-PSK
  75. pairwise=$pairwise
  76. group=$group
  77. psk=$skey
  78. }" > /etc/wpa_supplicant/wpa_supplicant.conf
  79.         rm .list.tmp 2>/dev/null
  80.         wpa_supplicant -B -Dwext -i $iface -c /etc/wpa_supplicant/wpa_supplicant.conf && dhclient wlan0
  81.         exit
  82.     else
  83.         #sets the wireless configuration for non WPA: essid, channel, mode, key, etc
  84.         iwconfig $iface essid \""$essid"\" channel $channel mode $mode key $key
  85.         #connects to wifi connection
  86.         dhclient $iface
  87.         exit
  88.     fi
  89. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement