Build a Network Analyzer in Bash: Monitor Connectivity, Ping Hosts, and Scan Ports from the Terminal
- Samul Black
- Jul 29
- 5 min read
In today’s connected world, the ability to quickly analyze network health from the command line is an invaluable skill for system administrators, developers, and power users alike. In this blog, you’ll learn how to build a fully functional network analyzer using nothing but Bash scripting. This project will demonstrate how to write a terminal-based tool capable of checking internet connectivity, resolving DNS records, measuring latency, displaying active network interfaces, scanning open ports on a given host, and even logging diagnostics for future use.
Everything you'll build uses standard Unix tools like ping, dig, awk, and /dev/tcp, so it runs natively on both Linux and macOS systems. This hands-on guide is ideal for anyone looking to strengthen their Bash scripting skills while creating something genuinely useful. By the end, you’ll have a versatile and customizable Bash script that can be run manually or scheduled to monitor and diagnose your network environment — all from the terminal.

Setting Up the Script
Before diving into the network analysis logic, let’s start by creating a dedicated workspace for our Bash script. This helps in keeping your project organized.
First, open your terminal and create a new directory:
mkdir bash-net-analyzer
cd bash-net-analyzer
touch net-analyzer.sh
chmod +x net-analyzer.sh
Now, open net-analyzer.sh in your favorite text editor and add the shebang line:
#!/bin/bash
This tells the system to interpret the script with Bash.
Step 1: Check Internet Connectivity
One of the most fundamental checks in a network diagnostic tool is confirming whether the machine is connected to the internet. We can do this by pinging a reliable public DNS server like Google’s 8.8.8.8.
#!/bin/bash
ping -c 1 8.8.8.8 &> /dev/null && \
echo "Internet: Connected" || \
echo "Internet: Disconnected"
Output:
Internet: Connected
Here, the -c 1 flag sends just one ping request. We redirect the output to /dev/null to silence it. The use of && and ||lets us print a success or failure message depending on the outcome.
Step 2: Resolve Domain Name (DNS Lookup)
Resolving a domain to its corresponding IP address is another crucial feature. This helps verify that DNS resolution is functioning properly.
read -p "Enter a domain to resolve: " domain
ip=$(dig +short "$domain" | head -n 1)
echo "Resolved IP for $domain: $ip"
If you’re on macOS and don’t have dig, you can replace it with:
ip=$(nslookup "$domain" | awk '/^Address: / { print $2 }')
Once you run this script the outputs will look like following:
Enter a domain to resolve: www.colabcodes.com
Resolved IP for www.colabcodes.com: 34.149.87.45
Step 3: Measure Latency with Ping
To get a sense of network speed, we can measure latency by pinging a domain multiple times and calculating the average round-trip time.
echo "Pinging $domain..."
avg_latency=$(ping -c 4 "$domain" | tail -1 | awk -F '/' '{print $5}')
echo "Average Latency: $avg_latency ms"
Output so far will look like :
Internet: Connected
Enter a domain to resolve: www.colabcodes.com
Resolved IP for www.colabcodes.com: 34.149.87.45
Pinging www.colabcodes.com...
Average Latency: 23.295 ms
Step 4: Show Active Network Interfaces
Knowing which interfaces are active and their IP addresses is key to understanding your machine's current connectivity status.
echo "Active Network Interfaces:"
ip -brief address
On macOS:
ifconfig | grep "inet " | grep -v 127.0.0.1
Output:
Active Network Interfaces:
inet 192.168.1.58 netmask 0xffffff00 broadcast 192.168.1.255
Step 5: Scan Open Ports (Using /dev/tcp)
Let’s now implement a basic port scanner to identify which common ports are open on a host. We'll scan ports 20 to 25.
read -p "Enter host to scan (IP or domain): " scan_host
for port in {20..25}; do
timeout 1 bash -c "echo >/dev/tcp/$scan_host/$port" 2>/dev/null &&
echo "Port $port is OPEN" || echo "Port $port is CLOSED"
done
This script uses Bash’s /dev/tcp virtual file to test TCP connections. We use timeout to avoid hanging on unresponsive ports.
Output:
Enter host to scan (IP or domain): 192.168.1.58
Port 20 is CLOSED
Port 21 is CLOSED
Port 22 is CLOSED
Port 23 is CLOSED
Port 24 is CLOSED
Port 25 is CLOSED
Step 6: Get Default Gateway / Routing Info
Knowing the default gateway helps in understanding the path your network traffic takes to reach the internet.
echo "Default Gateway:"
ip route show default | head -n 1
On macOS or if ip is unavailable:
netstat -rn | grep '^default'
Step 7: Log Output to a File
To make your network tool more robust, let’s add the ability to log all results to a file. The filename will be timestamped.
logfile="network-log-$(date +%F_%H-%M-%S).log"
{
echo "Network Analyzer Report - $(date)"
echo "----------------------------------"
ping -c 1 8.8.8.8 &> /dev/null && echo "Internet: Connected" || echo "Internet: Disconnected"
echo "Resolved IP for $domain: $ip"
echo "Average Latency to $domain: $avg_latency ms"
echo "Interfaces:"
ip -brief address
echo "Port Scan for $scan_host:"
for port in {20..25}; do
timeout 1 bash -c "echo >/dev/tcp/$scan_host/$port" 2>/dev/null &&
echo "Port $port is OPEN" || echo "Port $port is CLOSED"
done
echo "Default Route:"
ip route show default | head -n 1
} >> "$logfile"
Complete Bash Script - Network Analyzer
Here’s the final version of the full script with everything combined:
#!/bin/bash
ping -c 1 8.8.8.8 &> /dev/null && \
echo "Internet: Connected" || \
echo "Internet: Disconnected"
read -p "Enter a domain to resolve: " domain
ip=$(nslookup "$domain" | awk '/^Address: / { print $2 }')
echo "Resolved IP for $domain: $ip"
echo "Pinging $domain..."
avg_latency=$(ping -c 4 "$domain" | tail -1 | awk -F '/' '{print $5}')
echo "Average Latency: $avg_latency ms"
echo "Active Network Interfaces:"
ifconfig | grep "inet " | grep -v 127.0.0.1
read -p "Enter host to scan (IP or domain): " scan_host
for port in {20..25}; do
timeout 1 bash -c "echo >/dev/tcp/$scan_host/$port" 2>/dev/null &&
echo "Port $port is OPEN" || echo "Port $port is CLOSED"
done
echo "Default Gateway:"
netstat -rn | grep '^default'
logfile="network-log-$(date +%F_%H-%M-%S).log"
{
echo "Network Analyzer Report - $(date)"
echo "----------------------------------"
ping -c 1 8.8.8.8 &> /dev/null && echo "Internet: Connected" || echo "Internet: Disconnected"
echo "Resolved IP for $domain: $ip"
echo "Average Latency to $domain: $avg_latency ms"
echo "Interfaces:"
ifconfig | grep "inet " | grep -v 127.0.0.1
echo "Port Scan for $scan_host:"
for port in {20..25}; do
timeout 1 bash -c "echo >/dev/tcp/$scan_host/$port" 2>/dev/null &&
echo "Port $port is OPEN" || echo "Port $port is CLOSED"
done
echo "Default Route:"
netstat -rn | grep '^default'
} >> "$logfile"
Final Output:
Mac-mini bash-net-analyser % bash net-analyzer.sh
Internet: Connected
Enter a domain to resolve: www.colabcodes.com
Resolved IP for www.colabcodes.com: 34.149.87.45
Pinging www.colabcodes.com...
Average Latency: 23.880 ms
Active Network Interfaces:
inet 192.168.1.58 netmask 0xffffff00 broadcast 192.168.1.255
Enter host to scan (IP or domain): 24.249.87.45
Port 20 is CLOSED
Port 21 is CLOSED
Port 22 is CLOSED
Port 23 is CLOSED
Port 24 is CLOSED
Port 25 is CLOSED
Default Gateway:
default 192.168.1.1 UGScg en1
default fe80::%utun0 UGcIg utun0
default fe80::%utun1 UGcIg utun1
default fe80::%utun2 UGcIg utun2
default fe80::%utun3 UGcIg utun3
Conclusion
With just a few lines of Bash code and built-in tools, you've built a powerful network analyzer that can test connectivity, resolve domains, measure latency, scan ports, and log results — all from the terminal. This project not only strengthens your command-line and scripting skills but also equips you with a practical tool you can use or expand upon in real-world scenarios. Whether you’re debugging a home setup or exploring sysadmin basics, this Bash-based analyzer is a reliable starting point.
By combining fundamental networking commands with Bash’s scripting power, you've created a tool that’s lightweight, portable, and highly customizable. You can easily enhance it further — add email alerts for downtime, visualize the logs using charts, or automate the script with cron jobs. This hands-on project bridges the gap between scripting and real-world utility, showing how even simple scripts can yield robust network diagnostics.
Continue experimenting, and consider adding more features like traceroute mapping, bandwidth monitoring, or ICMP packet loss tracking to level up your analyzer even more!