Advertisement
constantin-net

wpa_connect_v2

Sep 17th, 2020 (edited)
1,625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.07 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. [ -z "$1" -o -z "$2" ] && echo "w_iface up/last/down" && exit 0
  4.  
  5. rm .list.tmp 2>/dev/null
  6. wpa_config="/etc/wpa_supplicant/wpa_supplicant.conf"
  7. last_ssid=$(cat $wpa_config | grep ssid=\" | awk -F'=' '{print $2}')
  8.  
  9. case $2 in
  10.     down)
  11.         pkill wpa_supplicant
  12.         dhclient $1 -x
  13.         ifconfig $1 down
  14.         exit 0
  15.         ;;
  16.     last)
  17.         pkill wpa_supplicant
  18.         dhclient $1 -x
  19.         ifconfig $1 down
  20.         ifconfig $1 up
  21.         if [[ -z `cat /etc/network/interfaces | grep $1` ]]; then
  22.             wpa_supplicant -B -Dwext -i $1 -c "$wpa_config"
  23.             dhclient $1
  24.         else
  25.             ifup $1
  26.         fi
  27.         exit 0
  28.         ;;
  29.     up)
  30.         pkill wpa_supplicant
  31.         dhclient $1 -x
  32.         #dhclient -r
  33.         ifconfig $1 up
  34.         iwlist $1 scan 2>/dev/null > .list.tmp
  35.         eval list=( $(cat .list.tmp | awk -F":" '/ESSID/{print $2}' | nl -w2 -n rz -s "=") )
  36.         #sets prompt
  37.         PS3="Choose wifi connection: "
  38.         #tests for number of wifi connections, exits if none
  39.         if [[ -z "${list[0]}" ]]; then
  40.             clear
  41.             echo "No available wifi connection"
  42.             exit 1
  43.         fi
  44.         #menu of wifi connections
  45.         select item in "${list[@]}"; do
  46.             cell=$(echo $item | awk -F"=" '{print $1}')
  47.             essid=$(echo $item | awk -F"=" '{print $2}')
  48.             echo " "
  49.             echo "$essid: Connecting..."
  50.             echo " "
  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 1
  68.             fi
  69.             #tests for encryption key
  70.             key=$(grep key: .list.tmp | sed 's/.*key://g')
  71.             if [[ $key == "on" ]]; then
  72.                 while true; do
  73.                     read -r -s -p "$essid: Enter password: " key
  74.                     if [ ${#key} -lt 8 ] || [ ${#key} -gt 32 ]; then
  75.                         echo "The password must be 8-32 characters"
  76.                         :
  77.                     else
  78.                         break
  79.                     fi
  80.                 done
  81.             fi
  82.             skey=$(wpa_passphrase "$essid" $key 2>/dev/null | sed -n 's/^[ \t]psk=//gp')
  83.             #checks encryption algorithm
  84.             IE=$(grep WPA .list.tmp | sed 's/.*: //g')
  85.             if [[ -n "$IE" ]]; then
  86.                 echo "ctrl_interface=/var/run/wpa_supplicant
  87. ap_scan=1
  88. network={
  89. ssid=\"$essid\"
  90. scan_ssid=0
  91. proto=WPA RSN
  92. key_mgmt=WPA-PSK
  93. pairwise=$pairwise
  94. group=$group
  95. psk=$skey
  96. }" > "$wpa_config"
  97.                 if [[ -z `cat /etc/network/interfaces | grep $1` ]]; then
  98.                     sudo pkill wpa_supplicant
  99.                     sudo dhclient -r
  100.                     sudo ifconfig $1 up
  101.                     wpa_supplicant -B -Dwext -i $1 -c "$wpa_config" &>/dev/null
  102.                     dhclient $1
  103.                 else
  104.                     ifup $1
  105.                 fi
  106.                 exit 0
  107.             else
  108.                 #sets the wireless configuration for non WPA: essid, channel, mode, key, etc
  109.                 sudo pkill wpa_supplicant
  110.                 sudo dhclient -r
  111.                 sudo ifconfig $1 up
  112.                 iwconfig $1 essid "$essid" channel $channel mode $mode key $key
  113.                 #connects to wifi connection
  114.                 dhclient $1
  115.                 exit 0
  116.             fi
  117.         done
  118.         ;;
  119.     *)
  120.         exit 1
  121. esac
  122.  
  123.  
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement