Exploring Port Scanning with Bash


As a sysadmin, one of your essential tasks is to ensure the security and accessibility of your network and servers. Port scanning is a vital technique that allows you to check the status of network ports, helping you identify potential vulnerabilities or issues. While there are numerous tools available for this purpose, knowing how to perform a basic port scan with Bash can be a valuable skill. In this blog post, we’ll walk through the process of creating a simple Bash script for port scanning.


Before we begin, make sure you have the following:

  • A Linux-based system (this tutorial is demonstrated on a Linux platform).
  • A basic understanding of Bash scripting.
  • Appropriate permissions for scanning the target system. Always ensure you have authorization before scanning any network.

Creating the Bash Script:

Let’s start by creating a Bash script for port scanning. Open your favorite text editor and follow these steps:

  1. Define the target host: Set the target_host variable to the IP address or domain name of the server you want to scan.
  1. Specify the port range: Define the range of ports you want to scan by setting the start_port and end_port variables.
  1. Perform the port scan: Use a loop to iterate through the specified port range and check if each port is open or closed on the target host.
   for ((port = $start_port; port <= $end_port; port++)); do
       (echo > /dev/tcp/$target_host/$port) 2>/dev/null && echo "Port $port is open" || echo "Port $port is closed"

Running the Script:

After saving the script, make it executable with the following command:

chmod +x portscanner.sh

To execute the script, run:


The script will loop through the specified port range and report the status of each port on the target host.

Responsible Usage:

It’s essential to emphasize the responsible and legal use of port scanning. Always obtain proper authorization before scanning any network. Unauthorized port scanning can have legal consequences.


In this blog post, we’ve explored the creation of a Bash script for port scanning. While this is a basic example, it serves as a foundation for understanding the concept. More advanced tools like Nmap offer comprehensive port scanning capabilities, but knowing how to perform a simple port scan with Bash is a valuable skill for sysadmins.

Port scanning is a critical aspect of network security and troubleshooting. By learning how to create a Bash script for port scanning, you’ll have a better understanding of network analysis and can enhance your sysadmin skills.

Feel free to adapt and expand upon this script to suit your specific needs and use cases. Remember, with great power comes great responsibility, so always scan networks with proper authorization and ethical considerations.

Happy scanning and stay secure!

Leave a Comment

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