Lazy Bash Scripting: Monitoring mode
Bash scripting can be a very useful and powerful way of automating a bit of the boring stuff for you. Over these small segments, we are going to be creating little scripts that can make all our life’s much easier.
For our first segment, we are going to be creating a small script that will change the MAC address of a specified network card and also puts the same card into monitoring mode.
To start off we are going to create a new file with the name Monitor.sh and open it up. At the start of any script, we all was want to let the computer know what type of file is dealing with. We do this by putting:
At the start of every script we write. Now the computer knows what type of file it is we can start writing in the commands. First, we are going to want to check that the script is running as root. If the script is not running as root then it will display an error message to the user and exit. We can do this with an if statement like shown below:
if [[ $EUID -ne 0 ]]; then echo "This script must be run as root" exit Fi
We can now go ahead and ask the user what network interface card they would like to put into monitoring mode, we can do this with the code:
read -p "Please select a NIC: " NIC
This will print Please select a NIC to the terminal and wait for the user to input a selection, the user’s input will then be stored in a variable called NIC. We can then use this variable to set up the monitoring mode and change the mac address. With the code:
ifconfig $NIC down macchanger -a $NIC | grep New iwconfig $NIC mode monitor iwconfig $NIC | grep Mode ifconfig $NIC up echo "Script Complete"
We have now finished the script, all we have to do now is save it and give the file executable permissions. We do this by opening up a terminal at the path where we have saved the file and running the command:
chmod +x Monitor.sh
We can now run the script and put are network interface card into monitoring mode with a new MAC address by running the command:
We have now created our first bash script, in the future segments we are going to be creating more bash scripts that can make our day to day tasks a lot easier. When we have created a couple of different scripts we will look at putting them all together into our own little toolkit.