#!/bin/bash
######################################################################
#Copyright (C) 2023 Kris Occhipinti
#https://filmsbykris.com
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation version 3 of the License.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program. If not, see .
######################################################################
config="/etc/wpa_supplicant/wpa_supplicant.conf"
function error(){
echo $*
exit 1
}
function get_device(){
device="$(ifconfig -a|grep wlp|cut -d\: -f1|head -n1)"
[[ $device ]] || error "No Device Found"
echo $device
}
function scan_ssid(){
[[ $device ]] || error "No Device Found"
ssid="$(sudo iwlist "$device" scan|grep ESSID|cut -d\" -f2|sort -u|fzf --prompt "Select a Network: ")"
[[ $ssid ]] || error "No SSID Selected"
}
function save_config(){
read -p "Enter Pass Phrase for Network: " pass
[[ $pass ]] || error "No Pass Phrase Entered"
[[ $ssid ]] || error "No SSID Selected"
config="$(sudo wpa_passphrase $ssid $pass)"
echo "$config"
echo ""
read -p "Add above to wpa_supplicant.conf [Y/n]? " confirm
[[ "$confirm" == "n" ]] && exit
echo $config |sudo tee -a $config
#reset permissions
sudo chmod 0600 /etc/wpa_supplicant/wpa_supplicant.conf
}
function connect2network(){
[[ $device ]] || error "No Device Found"
sudo ifconfig $device down
sudo ifconfig $device up
sudo wpa_supplicant -B -c $config -i $device
sudo ip address add 192.168.1.108/24 brd dev $device
sudo ip route add 192.168.1.1 dev $device
sudo ip route add default via 192.168.1.1 dev $device
echo "nameserver 8.8.8.8" |sudo tee -a /etc/resolv.conf
sudo dhcpclient $deivce
}
function new_network(){
get_device
scan_ssid
save_config
connect2network
test_connection
}
function test_connection(){
ip a
ping -c 3 192.168.1.1
ping -c 3 google.com
}
new_network
#read -p "Connect to Network [Y/n]?" connect
#[[ "$connect" != "n" ]] && connect2network
read -p "Press Enter to Continue."
cat $config