DEV Community

Cover image for Learn Bash Scripting With Me 🚀 - Day 6
Babs
Babs

Posted on

Learn Bash Scripting With Me 🚀 - Day 6

Day 6 - Ping Sweep

In case you’d like to revisit or catch up on what we covered on Day Five, here’s the link:

https://dev.to/babsarena/learn-bash-scripting-with-me-day-5-45di
Enter fullscreen mode Exit fullscreen mode

In this lab session, we will create a script that utilizes a for loop, as introduced in the previous post. This exercise will give you a clearer understanding of how for loops are used in practical scenarios. The script we will develop is particularly useful for professionals in penetration testing and cybersecurity.

The main objective of this script is to ping a set of IP addresses of your choice and determine which ones are online. For example, imagine needing to ping a list of over 200 computers to quickly identify which machines are active on the network—doing this manually would be extremely time-consuming.

We will begin by defining the interpreter at the top of the script using the shebang (#!/bin/bash). Additionally, ensure that the script file you create is executable—you should already be familiar with making a file executable by now.

The Script

The image above shows the script created, so I will be explaining the script below.

Explanation

#!/bin/bash
Enter fullscreen mode Exit fullscreen mode
  • The above is called a shebang, It tells the system to use the Bash shell to run this script.
#Creating a simple pingsweep script
Enter fullscreen mode Exit fullscreen mode
  • The line above is a comment. In Bash, any text that starts with # is ignored by the interpreter and is not executed as part of the script. Comments are used to provide explanations or notes within your code, making the script easier to understand and maintain for yourself and others.
echo “Please enter the first 3 IP address Octet for your environment as shown in the example e.g 10.10.1”
Enter fullscreen mode Exit fullscreen mode
  • The echo command above displays the specified message on the terminal when the script is run. In this case, it prompts the user to enter the first three octets of the IP addresses for their environment (e.g., 10.10.1).
read SUBNET
Enter fullscreen mode Exit fullscreen mode
  • The read command captures input from the user and stores it in a variable. In this case, the input provided by the user is saved to the variable SUBNET.
for IP in $(seq 1 200); do
Enter fullscreen mode Exit fullscreen mode
  • In this line, we define a new variable called IP. The $(seq 1 200) generates a sequence of numbers from 1 to 200, which the for loop will iterate over. The semicolon (;) is used to separate commands on the same line—it tells the shell that the for statement is complete, and the following do marks the start of the loop’s body.
ping -c2 $SUBNET.$IP
Enter fullscreen mode Exit fullscreen mode
  • The ping -c2 command sends two pings to the IP address to check if it is online.

  • $SUBNET is replaced with the first three parts of the IP address that the user entered.

  • The dot . separates the subnet from the last octet.

  • $IP is the last part of the IP address, which changes from 1 to 200 as the loop runs.

  • Together, this command pings each IP in the range like 10.10.1.1, 10.10.1.2, ..., up to 10.10.1.200.

done
Enter fullscreen mode Exit fullscreen mode
  • In Bash, done is used to mark the end of a for loop.

You can now run the script by entering the first three octets of your device’s IP address. The script will then check which other devices on your network are online.

The Output would look like something shown below:

NB - The output from the script displays the standard results you would get from running a ping command for all 200 IPs.

You can also stop the script at any time by pressing Ctrl + C.

Top comments (0)