Jump to content

[Script] Client Mode WiFi Check


NullNull
 Share

Recommended Posts

Hello everyone.
For the past few months i am using my pineapple as a kind of access point repeater with the help of "Wifi Manager" infusion (thanks Whistle Master!!! ). Wlan1 is connected to my home network on client mode and wlan0 is running an open, internet sharing, wireless netowork (from boot). The connection between my home network and my pineapple is really bad so every now and then the connection drops. I wrote this script to check for internet and network connection and if its down try to get it back on.

Maybe some of you will find it useful.

EDIT: I have done some changes after seeing you guys liked it so here is 1.1 Version

Change Log:

  • Fixed some output issues
  • Checks and brings up the device if down
  • Makes more checks regarding the usb inteface issues (i am having)
  • Countdown for the next check
  • Much better chances of grabing an ip if connection is lost

Unfortunately i dont have the time to bring this into an CLI infusion or the time to support one. Also i own a mark IV pineapple running 2.8.1 which i dont want to update to 3.0.0 ( sorry seb :P ) so developing a CLI infusion wont be easy. If anyone wants to bring it in an infusion is welcome! :)

#!/bin/bash

                       #check.sh   v-1.1
                       #By KiatoGS
sleep_time="30"        #Seconds between every check
device="wlan1"         #The device you are using for client mode
gateway="192.168.1.1"  #Your " home's " network gateway for network connection check

clear
echo
echo
echo -e " +---------------------------------------+"
echo -e "  This script will run a loop every \e[1;32m$sleep_time\e[0m "
echo -e "     seconds to check your client mode"
echo -e "      connection on \e[1;32m$device\e[0m pinging"
echo -e "         \e[1;32m$gateway\e[0m and \e[1;32m8.8.8.8\e[0m"
echo -e " +---------------------------------------+"
echo
echo
while :
do
    sleeptime=$sleep_time
    echo -n "[-] Checking for Internet connection..."
    ping -q -w 6 -c 1 8.8.8.8 > /dev/null 2>&1
    if [[ $? -eq 0 ]]; then
        echo -e "\t [\e[1;32mOK\e[0m]"
    else
        echo -e "\t [\e[1;31mFAILED\e[0m]"
        echo -n "[-] Checking for Network connection..."
        ping -q -w 6 -c 1 $gateway > /dev/null 2>&1
        if [[ $? -eq 0 ]]; then
            echo -e "\t [\e[1;32mOK\e[0m]"
            echo -e  "[-] Check your \" home's \" internet connection \e[1;31m!\e[0m"  #Nothing we can do :/
        else
            echo -e "\t [\e[1;31mFAILED\e[0m]"
            echo -n "[-] Checking for $device device status..."    #When i am using a usb wireless card for client mode there are times
                                                                   #that the usb card just disappears from my pineapple. Only a reboot
                                                                   #brings it back on.
                                        
            if [[ -z $(ifconfig | grep "$device") ]] || [[ -z $(iwconfig 2>&1 | grep "$device") ]] ; then
                echo -e "\t [\e[1;31mDOWN\e[0m]"
                echo -ne "[-] Bringing it back \"up\"..."
                ifconfig $device up > /dev/null 2>&1
                if [[ -z $(ifconfig | grep "$device") ]] ; then
                    echo -e "\t \t [\e[1;31mFAILED\e[0m]"
                else
                    echo -e "\t \t [\e[1;32mOK\e[0m]"
                fi
                if [[ -z $(iwconfig 2>&1 | grep "$device") ]] ; then
                    echo -e "[-] Sorry but $device is not responding... \t [\e[1;31mREBOOTING\e[0m]"
                    sleep 5
                    reboot
                    exit
                fi
            else
                echo -e "\t [\e[1;32mUP\e[0m]"
            fi
            echo -n "[-] Realising ip if any..."
            kill `ps -ax | grep udhcp | grep "$device" | awk {'print $1'}` 2> /dev/null
            echo -e "\t \t [\e[1;32mOK\e[0m]"
            echo -n "[-] Scanning APs..."
            iwlist "$device" scan > /dev/null 2>&1            #At all my tests i had better results executing iwlist twice.
            sleep 1                                           #Try to comment out the second one and tell my your opinion.
            iwlist "$device" scan > /dev/null 2>&1            #The loop must run an extra time (so basicaly iwlist is running again)
            echo -e "\t \t \t [\e[1;32mOK\e[0m]"              #in order udhcpc to get an ip. I cannot understand why :/
            echo -n "[-] Requesting ip..."
            udhcpc -R -n -i "$device" > /dev/null 2>&1
            if [[ -n $(ifconfig "$device" | grep "inet addr:") ]] && [[ -n $(route | grep "$device") ]] ; then
                sleep 1
                echo -e "\t \t \t [\e[1;32mOK\e[0m]"
            else
                echo -e "\t \t \t [\e[1;31mFAILED\e[0m]"
            fi
            sleeptime="1"
        fi
    fi
    if [[ $sleeptime != "1" ]] ; then                         #Just a small countdown
        while [ $sleeptime -ge 1 ]
        do
          echo -ne "[-] Checking again in: \t \t \t [$sleeptime] \r"
          sleep 1
          sleeptime=$[$sleeptime-1]
        done
    fi
done
Edited by KiatoGS
Link to comment
Share on other sites

I also agree, but i think you should set up the device as a user variable. For ease of use. As some wont know to change the bash script to read wlan2,wlan3, etc instead of wlan1. Maybe have it read it as a cl argument.

So device="wlan1" change to device=$1

As $1 is first commandline arg in bash.

Or set up another variable for the input arg one.

IPUTDEVICE=$1

device='wlan1'

Then do a if ! iwconfig | grep $(INPUTDEVICE) exit with message to say not a valid device etc just for handling incorrect device specified. or default to wlan1? And if no arg is passed, default to wlan1

else device=INPUTDEVICE

I'm rushed at the moment, otherwise i would have changed it to such and submitted to you a proper .patch as respect to opensource community. but thoae are my ideas / 2cents. If I get around to it later, I will submit to you a .patch or my modified script

Edited by datahead
Link to comment
Share on other sites

Here is my edit:

i dont need any credit for the edit either, all is claim to you, this is more a suggestion implementation. but it works fine :)

probably also should add a getopts for ease to change the device & gateway address aswell. but meh, ill let you change it if you want

inputdevice=$1


   clear
	if [[ -z $(iwconfig | grep -s $inputdevice) ]]
		then
			
			echo -e "\t \n \n $inputdevice is [\e[1;31mNOT VALID\e[0m]"
			echo -e "\t [\e[1;31mPlease re-run the script with a valid device\e[0m]"
                        echo -e "\t [\e[1;31mor else $device will be assumed\e[0m]"
			sleep 5

		else
                        clear
			$device = $inputdevice
	fi

the full script

#!/bin/bash

			#By KiatoGS
sleep_time="30"		#Seconds between every chec
device="wlan1"		#The device you are using for client mode
gateway="192.168.1.1"	#Your " home's " network gateway for network connection check

inputdevice=$1


clear
    if [[ -z $(iwconfig | grep -s $inputdevice) ]]
        then
            
            echo -e "\t \n \n $inputdevice is [\e[1;31mNOT VALID\e[0m]"
            echo -e "\t [\e[1;31mPlease re-run the script with a valid device\e[0m]"
            echo -e "\t [\e[1;31mor else $device will be assumed\e[0m]"
            sleep 5

        else
clear
            $device = $inputdevice
    fi






clear 
echo
echo
echo -e " +---------------------------------------+"
echo -e "  This script will run a loop every \e[1;32m$sleep_time\e[0m "
echo -e "     seconds to check your client mode"
echo -e "      connection on \e[1;32m$device\e[0m pinging"
echo -e "         \e[1;32m$gateway\e[0m and \e[1;32m8.8.8.8\e[0m"
echo -e " +---------------------------------------+"
echo
echo




while :
do
	sleeptime=$sleep_time
	echo -n "[-] Checking for Internet connection..."
	ping -q -w 6 -c 1 8.8.8.8 > /dev/null 2>&1			#Ping google
	if [[ $? -eq 0 ]]; then
		echo -e "\t [\e[1;32mOK\e[0m]"
	else
		echo -e "\t [\e[1;31mFAILED\e[0m]"
		echo -n "[-] Checking for Network connection..."
		ping -q -w 6 -c 1 $gateway > /dev/null 2>&1		#Ping gateway
		if [[ $? -eq 0 ]]; then
			echo -e "\t [\e[1;32mOK\e[0m]"
			echo -e  "[-] Check your \" home's \" internet connection \e[1;31m!\e[0m"
		else
			echo -e "\t [\e[1;31mFAILED\e[0m]"
			echo -n "[-] Checking for $device device..."	#When i am using a usb wireless card for client mode there are times
									#that the usb card just disappears from my pineapple. Only a reboot
									#brings it back on.
			if [[ -z $(iwconfig | grep "$device") ]] ; then
				echo -e "Sorry but $device doesn't seem to be up... \t \t [\e[1;31mREBOOTING\e[0m]"
				sleep 5
				reboot
			else
				echo -e "\t \t [\e[1;32mOK\e[0m]"
				echo -n "[-] Realising ip if any..."
				kill `ps -ax | grep udhcp | grep "$device" | awk {'print $1'}` 2> /dev/null
                                echo -e "\t \t \t [\e[1;32mOK\e[0m]"
				echo -n "[-] Scanning APs..."
				iwlist "$device" scan > /dev/null 2&>1
				sleep 4
				echo -e "\t \t \t [\e[1;32mOK\e[0m]"
				echo -n "[-] Requesting ip..."
				udhcpc -R -n -i "$device" > /dev/null 2&>1
				if [[ -n $(ifconfig "$device" | grep "inet addr:") ]] && [[ -n $(route | grep "$device") ]] ; then
					sleep 1
					echo -e "\t \t \t [\e[1;32mOK\e[0m]"
				else
					echo -e "\t \t \t [\e[1;31mFAILED\e[0m]"
				fi
				sleeptime="1"
			fi
		fi
	fi
	sleep $sleeptime
done
Edited by datahead
Link to comment
Share on other sites

  • 4 weeks later...

I'm presently using this script with DataHead's edit added. Seems to work so far.

However, my use-case is a bit different from that for which it is designed. I live full-time in a camper, and move around quite a bit. As a result, I'm not always on the same network, and of course, the $gateway variable would likely change with it. As well, I use a USB wlan2 when signal strengths require it, and unplug for power savings when they don't, so wlan1 or wlan2 would be a variable that changes.

Here is my semi-educated idea of how it worx:

1. script pings 8.8.8.8, if good, sleeps till next run. Or,

2. pings $gateway next, if good, tells you to check house AP's WAN connection. Or,

3. checks for existence of the interface it believes it should see, and if present, reconnects. Or,

4. Reboots pineapple

And here is how I envision it improved, and maybe working with the Connect Infusion:

1. Script runs (in my case, likely at startup), somehow AUTO-determines appropriate $interface and $gateway, pings 8.8.8.8 (or any other public IP), if good, sleeps until next run. Or,

2. pings $gateway, if good, tells you to check house AP's WAN, and runs "pineapple infusion connect -W" or some other reconnection routine with some mechanism to block current AP from being connected to for this run. Or,

3. checks for existence of $interface, and if available, runs "pineapple infusion connect -W" or other reconnection routine, starts again at step 1. Or,

4. Asks if another $interface should be used, or maybe even switches on own to first (or maybe last) available, and starts over.

I've VERY seldom seen any need to reboot the pineapple for interface detection, but we might need to keep a step 5 for worst-case reboot.

I'd love to see this script work in conjunction with the Connect Infusion, since this does much of what it does not, and vice versa. I've already posted in the Connect support thread very similarly, but wanted to bring some of my non-Connect-related suggestions here as well.

Edit: Seems the Connect infusion does much more than I realized. It already takes care of gateway auto-detect and connection checking. Between the 2, I think the only thing missing is an option to prevent associating with no-Internet AP's.

Edited by patriotek
Link to comment
Share on other sites

  • 5 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...