Skip to content

Instantly share code, notes, and snippets.

@nicnocquee
Last active April 26, 2022 04:39
Show Gist options
  • Save nicnocquee/9e030487c6f02cef5909 to your computer and use it in GitHub Desktop.
Save nicnocquee/9e030487c6f02cef5909 to your computer and use it in GitHub Desktop.
Script to autoconnect wifi in Ubuntu (linux)

Autoconnect wifi ubuntu

#!/bin/bash

wlan=`/sbin/ifconfig wlan0 | grep inet\ addr | wc -l` 
if [ $wlan -eq 0 ]; then
service network-manager restart 
else
echo WIFI IS UP
fi

Save the script and make it executable.

sudo chmod +x filename.sh

Now there are several ways of making sure that our script is being executed every x minutes. The easiest way of accomplishing that I think is by using the command watch. sudo su watch -n 600 sh filename.sh

What it does is execute our filename.sh script every 600 seconds. Or you implement a so called cron job

sudo crontab -e

Add the following

PATH=/usr/sbin:/usr/bin:/sbin:/bin
*/5 * * * * sh /home/username/filename.sh

*/5 * * * * means that the task will run every 5 minutes.

source

@andymule
Copy link

format has changed slightly in five years. Here's the revision:

wlan=/sbin/ifconfig wlan0 | grep inet | wc -l

@megablue
Copy link

megablue commented Apr 26, 2022

it doesn't really work on newer ubuntu
I've modified the script a bit for newer ubuntu.

#!/bin/bash
wlan=`/sbin/ifconfig wlp4s0 | grep inet | wc -l` 
if [ $wlan -eq 0 ]; then
	systemctl restart NetworkManager 
else
	echo WIFI IS UP
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment