Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #v2.3
- [ -z "$1" -o -z "$2" ] && echo "w_iface in_essid/last/down/hidden" && exit 0
- wpa_config_dir="$HOME/.config/wpa_supplicant"
- wpa_config="$HOME/.config/wpa_supplicant/wpa_supplicant.conf"
- last_ssid=$(cat $wpa_config | grep ssid=\" | awk -F'=' '{print $2}')
- case $2 in
- down)
- echo " "
- echo "$last_ssid: Disconnecting..."
- echo " "
- fsudo -c | sudo -S -p '' pkill wpa_supplicant
- sudo dhclient $1 -x
- sudo ifconfig $1 down
- sleep 1
- exit 0
- ;;
- last)
- echo " "
- echo "$last_ssid: Connecting..."
- echo " "
- fsudo -c | sudo -S -p '' dhclient $1 -x
- sudo pkill wpa_supplicant
- sudo ifconfig $1 down
- sudo ifconfig $1 up
- if [[ -z `cat /etc/network/interfaces | grep $1` ]]; then
- sudo wpa_supplicant -B -Dwext -i $1 -c "$wpa_config" &>/dev/null
- sudo dhclient -1 $1
- if [ $? -eq 0 ]; then
- echo "Success"
- sleep 1
- else
- echo "Fail"
- sleep 1
- fi
- else
- sudo ifup $1
- fi
- exit 0
- ;;
- hidden)
- echo " "
- echo "Hidden network..."
- echo " "
- fsudo -c | sudo -S -p '' dhclient $1 -x
- sudo pkill wpa_supplicant
- sudo ifconfig $1 down
- sudo ifconfig $1 up
- echo -n "Enter SSID: "
- read essid
- channel="auto"
- mode="managed"
- 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
- skey=$(wpa_passphrase "$essid" $key 2>/dev/null | sed -n 's/^[ \t]psk=//gp')
- echo "ctrl_interface=/var/run/wpa_supplicant
- ap_scan=1
- network={
- ssid=\"$essid\"
- scan_ssid=1
- proto=WPA RSN
- key_mgmt=WPA-PSK
- pairwise=CCMP TKIP
- group=CCMP TKIP
- psk=$skey
- }" > "$wpa_config"
- if [[ -z `cat /etc/network/interfaces | grep $1` ]]; then
- sudo wpa_supplicant -B -Dwext -i $1 -c "$wpa_config"
- sudo dhclient $1
- if [ $? -eq 0 ]; then
- echo "Success"
- sleep 1
- else
- echo "Fail"
- sleep 1
- fi
- else
- sudo ifup $1
- fi
- exit 0
- ;;
- *)
- md5name=$(echo -n $2 | md5sum | cut -d" " -f1)
- fsudo -c | sudo -S -p '' dhclient $1 -x
- sudo pkill wpa_supplicant
- sudo dhclient -r
- sudo ifconfig $1 up
- cell=$(echo $2 | awk -F"=" '{print $1}')
- essid=$(echo $2 | awk -F"=" '{print $2}')
- if [[ -z `find "$wpa_config_dir" -name "$md5name"` ]]; then
- echo " "
- echo "$essid: Connecting..."
- echo " "
- scan_parms="$(sudo iwlist $1 scan)"
- scan_parms=$(echo "$scan_parms" | sed -ne "/Cell $cell/,/Cell/ p" | sed 's/^[ \t]*//')
- channel=$(echo "$scan_parms" | grep Channel: | sed 's/.*Channel://g')
- pairwise=$(echo "$scan_parms" | grep -m 1 Pairwise | sed 's/.* : //g')
- group=$(echo "$scan_parms" | grep -m 1 Group | sed 's/.* : //g')
- mode=$(echo "$scan_parms" | grep Mode | sed 's/.*Mode://g')
- if [[ "$mode" == "Master" ]]; then
- mode="managed"
- else
- echo "Cannot connect"
- sleep 2
- exit
- fi
- key=$(echo "$scan_parms" | grep key: | 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')
- IE=$(echo "$scan_parms" | grep WPA | 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 wpa_supplicant -B -Dwext -i $1 -c "$wpa_config" &>/dev/null
- sudo dhclient -1 $1
- if [ $? -eq 0 ]; then
- echo "Success"
- cp "$wpa_config" "$wpa_config_dir"/"$md5name"
- sleep 1
- else
- echo "Fail"
- sleep 1
- fi
- else
- sudo ifup $1
- fi
- exit 0
- else
- sudo iwconfig $1 essid "$essid" channel $channel mode $mode key $key
- sudo dhclient $1
- exit 0
- fi
- else
- echo " "
- echo "$essid: Connecting..."
- echo " "
- sudo wpa_supplicant -B -Dwext -i $1 -c "$wpa_config_dir"/"$md5name" &>/dev/null
- sudo dhclient -1 $1
- if [ $? -eq 0 ]; then
- echo "Success"
- cp "$wpa_config_dir"/"$md5name" "$wpa_config"
- sleep 1
- exit 0
- else
- echo "Fail"
- rm "$wpa_config_dir"/"$md5name"
- sleep 1
- exit 0
- fi
- fi
- esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement