Advertisement
constantin-net

wpa_connect_v1

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