constantin-net

wpa_connect_v2

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