#!/bin/bash # brezular v0.1 function show_choices { echo -e "\n*** Script configures wifi network, select from options below ***\n" echo -e "a) Change SSID" echo -e "b) Change WPA passphrase" echo -e "c) Restart wifi network" echo -e "q) Quit script\n" } function get_credentials { ssid="$(grep "^ssid" /usr/local/etc/hostapd.conf | cut -d "=" -f2)" passphrase="$(grep "^wpa_passphrase" /usr/local/etc/hostapd.conf | cut -d "=" -f2)" } function my_hostapd { [ "$1" == 'restart' ] && /usr/bin/pkill hostapd sleep 3 /usr/local/bin/hostapd -B /usr/local/etc/hostapd.conf 1>/dev/null } function check_hostapd_pid { ps -ef | grep hostapd | grep -v grep 1>/dev/null [ "$?" == 0 ] && my_hostapd restart || my_hostapd } function check_length { read word len=${#word} until [[ "$len" -ge "$2" && "$len" -le "$3" ]]; do echo -en "\nEnter $4 (min $2, max $3 chars): " read word len=${#word} done if [ "$1" == 'newssid' ]; then newssid="$word" elif [ "$1" == 'newpassphrase' ]; then newpassphrase="$word" fi } uid="$UID" [ "$uid" != 0 ] && echo -e "\nRun '$0' as root, exiting" && exit 1 while true; do show_choices read val case $val in a) get_credentials echo -en "\nEnter new SSID (min 1, max 32 chars): " check_length newssid 1 32 "new SSID" sed -i "s/ssid=$ssid/ssid=$newssid/g" /usr/local/etc/hostapd.conf check_hostapd_pid /usr/bin/filetool.sh -b 1>/dev/null echo -e "\n*** SSID changed successfully ***" ;; b) get_credentials echo -en "\nEnter new WPA passphrase (min 8, max 64 chars): " check_length newpassphrase 8 64 "new WPA passphrase" sed -i "s/wpa_passphrase=$passphrase/wpa_passphrase=$newpassphrase/g" /usr/local/etc/hostapd.conf check_hostapd_pid /usr/bin/filetool.sh -b 1>/dev/null echo -e "\n*** WPA passphrase changed successfully ***" ;; c) my_hostapd restart echo -e "\n*** Wifi network restarted successfully ***" ;; q) exit 0 ;; *) echo -e "\n*** Please, enter valid option ***" ;; esac done