Skip to content

Instantly share code, notes, and snippets.

@this-is-r-gaurav
Last active October 10, 2021 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save this-is-r-gaurav/2128f35830e6a759a7d6599eecc6748a to your computer and use it in GitHub Desktop.
Save this-is-r-gaurav/2128f35830e6a759a7d6599eecc6748a to your computer and use it in GitHub Desktop.
wifi-connect
#!/usr/bin/env bash
ProgName=$(basename $0)
function main(){
# Main Caller Function
parse_args "$@"
}
function parse_args(){
sub_command=$1;
case $sub_command in
"" | "-h" | "--help")
print_manual
;;
"-d" | "--debug" )
declare DEBUG=true
sub_command=$2
shift
;&
"connect" | "disconnect" | "status" | "scan" )
shift;
EXEC_ID=$(id -u)
if [[ $EXEC_ID != 0 ]];then
>&2 echo "Execute with sudo or run as root"
exit 1
fi
sub_${sub_command} "$@"
;;
"ip" )
shift;
sub_${sub_command} "$@"
;;
*)
>&2 echo "Command Not Found"
>&2 print_manual
;;
esac
}
###############
# SubCommands #
###############
function sub_disconnect(){
killall wpa_supplicant
return $?
}
function sub_status() {
if [[ -z $device_to_use ]];then
find_wireless_iface
fi
device_unconnected $device_to_use
if [[ $? == 0 ]];then
>&2 echo "No access to internet"
exit 1
else
>&2 echo "Connected to internet"
exit 0
fi
}
function sub_scan(){
if [[ -z $device_to_use ]];then
find_wireless_iface
fi
OLD_IFS=$IFS
IFS=$'\n'
SSIDS=($(scan_devices $device_to_use))
if [[ -z $SSIDS ]]; then
>&2 echo "No Wifi Device Found"
exit 1
fi
IFS=$OLD_IFS
for i in "${!SSIDS[@]}";do
printf "%s. %s\n" "$(( $i + 1 ))" "${SSIDS[$i]}"
done
return 0
}
function sub_ip(){
PARSED_ARGUMENTS=$(getopt -n ip -o ie -l internal,external -- "$@")
VALID_ARGUMENTS=$?
if [[ "$VALID_ARGUMENTS" != "0" ]];then
print_manual
exit 1
fi
eval set -- "$PARSED_ARGUMENTS"
while :
do
case "$1" in
-i | --internal )
declare INTERNAL=true
shift;
;;
-n | --external )
declare EXTERNAL=true
shift;
;;
-- )
shift;
break
;;
esac
done
if [[ -z $device_to_use ]];then
find_wireless_iface
fi
is_iface_up $device_to_use;
if [[ $? != 0 ]];then
>&2 echo "Wireless device $device_to_use is down"
exit $?
fi
device_unconnected $device_to_use;
if [[ $? == 0 ]];then
>&2 echo "Either you are not connected to router or not reachable to internet"
exit 1
fi
INTERNAL_IP=$(ip a show $device_to_use 2>/dev/null | grep -w inet | awk '{print $2}' | cut -f1 -d'/')
EXTERNAL_IP=$(dig +short myip.opendns.com @resolver1.opendns.com 2>/dev/null)
if [[ -z $INTERNAL && -z $EXTERNAL ]];then
INTERNAL=true
EXTERNAL=true
fi
if [[ $INTERNAL == true ]];then
echo "INTERNAL IP: $INTERNAL_IP"
fi
if [[ $EXTERNAL == true ]];then
echo "EXTERNAL IP: $EXTERNAL_IP"
fi
}
function sub_connect() {
PARSED_ARGUMENTS=$(getopt -n connect -o isp -l interactive,ssid,pass -- "$@")
VALID_ARGUMENTS=$?
if [[ "$VALID_ARGUMENTS" != "0" ]];then
print_manual
exit 1
fi
eval set -- "$PARSED_ARGUMENTS"
while :
do
case "$1" in
-i | --interactive )
declare INTERACTIVE=true
shift;
;;
-s | --ssid )
declare SSID="$1"
shift;
;;
-p | --pass )
declare PASS="$1"
shift;
;;
-- )
shift;
break
;;
esac
done
if [[ -n $DEBUG ]];then
echo "Finding wireless iface"
fi
find_wireless_iface
if [[ $INTERACTIVE == "true" ]];then
sub_scan
printf "Which device you want to connect to? (Enter Serial No): "
read ssid_index
ssid_to_connect="${SSIDS[$((ssid_index - 1))]}"
printf "Enter Password: "
read -s password
else
if [[ -z $SSID || -z $PASS ]];then
>&2 echo "--ssid and --pass are required if --interactive is not provided"
print_manual
exit 1
fi
ssid_to_connect="$SSID"
password="$PASS"
fi
printf "\nConnecting ${ssid_to_connect}\n"
connect_to_wifi "$ssid_to_connect" "$password" "$device_to_use"
}
####################
# HELPER FUNCTIONS #
####################
function connect_to_wifi(){
if [[ -n $DEBUG ]];then
cat << EOF
SSID: $1
PASSWORD: $2
IFACE: $3
EOF
fi
base_path=/etc/wpa_supplicant/devices
if [[ ! -d $base_path ]];then
mkdir -p $base_path
fi
file_name=$( echo -n "$1" | sed 's#\s#_#g')
wpa_existing_processes="$(ps -ax | grep wpa_supplicant | wc -l)"
if [[ $wpa_existing_processes -gt 1 ]];then
sub_disconnect
fi
wpa_passphrase "${1}" "$2" > ${base_path}/${file_name}.yaml
wpa_supplicant -B -D wext -i $3 -c ${base_path}/${file_name}.yaml &> /dev/null
if [[ $? == 0 ]];then
dhclient -r && sleep 5 && dhclient &> /dev/null
if [[ $? == 0 ]]; then
ping -c 1 8.8.8.8 &> /dev/null
if [[ $? == 0 ]];then
echo Connected to internet
return 0
else
echo Unable to reach internet, check your router
exit 1
fi
fi
else
>&2 echo "Unable to connect to wifi. Developer could help you troubleshoot"
exit 1
fi
}
function is_iface_up(){
ip link show up | grep $1 &> /dev/null
return $?
}
function scan_devices(){
is_iface_up $1;
l_status=$?
if [[ $l_status != 0 ]]; then
ip link set $1 up
if [[ $? != 0 ]];then
>&2 echo "Failed to start device"
exit 1
fi
fi
iw dev $1 scan 2>/dev/null | grep SSID | sed 's#\sSSID: ##g' | sort -u
return $?
}
function device_unconnected(){
# To Check whether wireless device not connected
if [[ -z $1 ]];then
>&2 echo "Device Not Provided"
exit 1
fi
device_status=$(iw dev $1 link 2> /dev/null)
if [[ "${device_status^^}" == "NOT CONNECTED." ]];then
return 0
fi
return 1
}
function find_wireless_iface(){
devices=($(ls /sys/class/net | grep wl))
if [[ -z $devices ]];then
>&2 echo "No Wireless interface detected"
exit 1
fi
device_to_use="${devices[0]}"
return 0
}
###########
# Manuals #
###########
function print_manual(){
cat << EOF
USAGE: $ProgName [GLOBAL_ARGS] SUB_COMMAND [COMMAND_ARGS]
GLOBAL_ARGS:
--debug -d To Print Debug statement
--help -h Print this manual
SUB_COMMANDS:
connect To connect wifi to router
disconnect To disconnect with existing router
ip To know your ip address
scan To scan nearby router.
status To know current status of the wireless router
COMMAND_ARGS:
1. connect Args
--interactive -i To scan and connect interactively
--ssid -s To provide ssid name [required if --interactive is not provided]
--pass -p To provide
2. ip
--internal -i To print internal ip
--external -e To print external ip
EOF
}
###################################### Execution start from Here ###################################
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment