Bash to Automate your Environment

6 months ago

When it comes to achieving something, there is nothing better than it happening on its own. Living in such a busy world lights up the need for ordinary and regular tasks completing on its own. The time when everything was a manual work is now gone, and we have upgraded to the automating environment. Everything we need is now a matter of mere seconds and substantially low efforts. From accessing websites to creating one, everything these days requires fewer strains, owing to various advanced technologies present nowadays like machine learning, artificial intelligence, and more.

When it comes to automating a system, we once in a few days stumble across a scripting language named "Bash." The bash scripting language, which stands for "Bourne Again SHell," and also known as "Shell" scripting language, is a UNIX OS embedded command line and scripting language. This language is a highly advanced scripting language, which is a result of Brian Fox's hard work towards the GNU project. Bash has been the default language of most of the Linux distributions, including Ubuntu, ArchLinux, and macOS.

When we talk about automation, bash scripting is always the goto language for many professionals and experts after python programming language. A language that mainly revolves around the Linux-based operating system development, bash, has laid out quite an impact in the cybersecurity environment. Where most of the web servers are Linux-based, "bash" is something that comes quite in handy when handling with those servers. Be it accessing or connecting to a server, scanning a server, or creating a server, the bash scripting language is advanced and integrated enough to enable a user to complete every task.

For instance, we can write a simple script using the bash scripting language to ping out to a web server or web application and extract the IP address of that web existence. The script could be as following:

if [ "$1" == "" ]
echo "You forgot an IP address!"
for ip in `seq 1 254`; do
ping -c 10 $1.$ip | grep "64 bytes" | cut -d " " -f 4 | tr -d ":" &

The above script will take an IP address as an input using the command "./<script_name> <first_3_IP_octets>" and will loop through the last octet ranging from 1 to 254, which is the range of an ordinary IPv4 address. Upon iteration, the ping statement will send packets to all the possible IP addresses and determine whether an active network device has an IP address or not. Generally, a ping request generates a lot of data as an output. This script comes into the picture to extract just the IP address of active devices and not the unnecessarily generated data.

Without going into deep ends of the explanation of the above script, I will evaluate the primary purpose. Why the need for this script? What is the point of writing all these commands into the bash script? It all comes down to two things, and that is are reusability and automation. One can do many things to achieve a goal, and for that, it could be a requirement to enter long commands, which could essentially be a tad bit hectic. Therefore, a script comes into play. Instead of writing down a long list of commands to achieve a goal repeatedly, we can write it down once, and it will do the deed for us automatically and recursively by a single execution.