Wifi reboot using automatic shell script on mac for irregular internet connection

I am running it on Mac OS X Yosemite 10.10.1

These days at my home I have a flaky internet connection. I am getting disconnected in every few minutes and had to turn off my wifi and switch it back on. Its very frustrating when I am watching a video, or downloading something. As it took a lot of time almost couple of minutes or more to get back to normal working again.

So to fix this temporarily I wrote below bash script and running it automatically at system boot. All it does is ping an ip every 5 seconds and if it fails to get a success response, it reboots the wifi on my mac. If it fails for 3 consecutive times then I am stoping this script. You can change to variable named ‘maxRebootTry’ according to your needs. Also you can limit of the number times of times you want to try ping by passing numeric count as an argument during execution time of the script. Example ./wifi.sh 100 , for 100 times ping. By default it is infinite

Step 1) Open Terminal

Step 2) sudo nano wifi.sh

Step 3) Copy & Paste below code. Then save(control+o -> enter) & exit (control+x)

maxPing=${1-0}

if [[ maxPing -ne 0 ]] ; then

        echo "Pinging $maxPing times "

else

        echo "Pinging unlimited times "

fi

#echo 'Begin ping'

#ping -i 5 8.8.8.8

((maxRebootTry=3))

((count=1))

while [ $count -ne 0 ] && [ $maxRebootTry -ne 0 ] ; do

        ping -c 1 8.8.8.8

        rc=$?

        echo "RESULT:$rc"

        if [[ $rc -eq 0 ]] ; then

                #say "good internet"

                ((maxRebootTry = 3))

                sleep 5

        else

                networksetup -setairportpower en0 off

                say "No internet connection. Trying wifi reboot"

                networksetup -setairportpower en0 on

                sleep 10

                say "wifi Reboot successful. Trying to connect again"

                ((maxRebootTry = maxRebootTry - 1))

        fi

        if [[ maxPing -ne 1 ]] ; then

                ((maxPing = maxPing - 1))

        else

                ((count = 0))

        fi

done

 

Step 4) Run it on terminal. Either pass argument for number of ping tries, or don’t pass anything for infinite pings

./wifi.sh 10

./wifi.sh

 

Step 5) Convert to Application (.app file) [Image 1]

Open Automater, New Document

Choose ‘Application’

Under Library -> Utilities -> click ‘Run Shell Script’ -> Either copy your code, or just write ./wifi.sh in the script option provided.

Save it as Application, and it would be save as .app file

Now, you can it like any other Mac application, by searching it in spotlight.

 

Step 6) Automation [Image 2]

Open Settings -> ‘User & Groups’ -> Current User -> Login Items

Click Add + and browse your .app.

Your application will now startup on its own upon your macbook reboot.

 

Step 7) Check it in status bar running successfully. Now your wifi will reboot itself if it needs to in case of lost packets. [Image 2]

 

 

Use Automater to convert shell script to application
Use Automater to convert shell script to application

2) startup mac program

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *