Write a bash script to configuring basic network connectivity

In this article, we are going to configure basic network connectivity using wpa_supplicant.

Prerequisites

Besides having a terminal open, we need to remember a few concepts:

  • Check whether wpa_supplicant is installed or not.
  • You should know the SSID and password.
  • Remember that you have to run your program as a root user.

How to do it

Create a script called wifi_conn.sh and write the following code in it:

#!/bin/bash 
ifdown wlan0 
rm /etc/network/interfaces 
touch /etc/network/interfaces 
echo 'auto lo' >> /etc/network/interfaces 
echo 'iface lo inet loopback' >> /etc/network/interfaces 
echo 'iface eth0 inet dhcp' >> /etc/network/interfaces 
echo 'allow-hotplug wlan0' >> /etc/network/interfaces 
echo 'iface wlan0 inet manual' >> /etc/network/interfaces 
echo 'wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf' >> /etc/network/interfaces 
echo 'iface default inet dhcp' >> /etc/network/interfaces 
rm /etc/wpa_supplicant/wpa_supplicant.conf 
touch /etc/wpa_supplicant/wpa_supplicant.conf 
echo 'ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev' >> /etc/wpa_supplicant/wpa_supplicant.conf 
echo 'update_config=1' >> /etc/wpa_supplicant/wpa_supplicant.conf 
wpa_passphrase $1 $2 >> /etc/wpa_supplicant/wpa_supplicant.conf 
ifup wlan0

How it works

Execute the script using sudo:

sudo bash wifi_conn.sh <SSID> <password>

0 Comments

Submit a Comment

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

Related Articles