Advertisement
metalx1000

wpa_supplicant wifi script

Nov 21st, 2023 (edited)
1,437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.48 KB | None | 0 0
  1. #!/bin/bash
  2. ######################################################################
  3. #Copyright (C) 2023  Kris Occhipinti
  4. #https://filmsbykris.com
  5.  
  6. #This program is free software: you can redistribute it and/or modify
  7. #it under the terms of the GNU General Public License as published by
  8. #the Free Software Foundation version 3 of the License.
  9.  
  10. #This program is distributed in the hope that it will be useful,
  11. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #GNU General Public License for more details.
  14.  
  15. #You should have received a copy of the GNU General Public License
  16. #along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. ######################################################################
  18.  
  19. config="/etc/wpa_supplicant/wpa_supplicant.conf"
  20.  
  21. function error(){
  22.   echo $*
  23.   exit 1
  24. }
  25.  
  26. function get_device(){
  27.   device="$(ifconfig -a|grep wlp|cut -d\: -f1|head -n1)"
  28.   [[ $device ]] || error "No Device Found"
  29.   echo $device
  30. }
  31.  
  32. function scan_ssid(){
  33.   [[ $device ]] || error "No Device Found"
  34.   ssid="$(sudo iwlist "$device" scan|grep ESSID|cut -d\" -f2|sort -u|fzf --prompt "Select a Network: ")"
  35.   [[ $ssid ]] || error "No SSID Selected"
  36. }
  37.  
  38. function save_config(){
  39.   read -p "Enter Pass Phrase for Network: " pass
  40.   [[ $pass ]] || error "No Pass Phrase Entered"
  41.   [[ $ssid ]] || error "No SSID Selected"
  42.   config="$(sudo wpa_passphrase $ssid $pass)"
  43.   echo "$config"
  44.   echo ""
  45.   read -p "Add above to wpa_supplicant.conf [Y/n]? " confirm
  46.   [[ "$confirm" == "n" ]] && exit
  47.   echo $config |sudo tee -a $config
  48.  
  49.   #reset permissions
  50.   sudo chmod 0600 /etc/wpa_supplicant/wpa_supplicant.conf
  51. }
  52.  
  53. function connect2network(){
  54.   [[ $device ]] || error "No Device Found"
  55.   sudo ifconfig $device down
  56.   sudo ifconfig $device up
  57.  
  58.   sudo wpa_supplicant -B -c $config -i $device
  59.   sudo ip address add 192.168.1.108/24 brd dev $device
  60.   sudo ip route add 192.168.1.1 dev $device
  61.   sudo ip route add default via 192.168.1.1 dev $device
  62.   echo "nameserver 8.8.8.8" |sudo tee -a /etc/resolv.conf
  63.  
  64.   sudo dhcpclient $deivce
  65. }
  66.  
  67. function new_network(){
  68.   get_device
  69.   scan_ssid
  70.   save_config
  71.   connect2network
  72.   test_connection
  73. }
  74.  
  75. function test_connection(){
  76.   ip a
  77.   ping -c 3 192.168.1.1
  78.   ping -c 3 google.com
  79. }
  80.  
  81. new_network
  82. #read -p "Connect to Network [Y/n]?" connect
  83. #[[ "$connect" != "n" ]] && connect2network
  84.  
  85. read -p "Press Enter to Continue."
  86. cat $config
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement