Skip to content

Network_diagnostics

This chapter covers network diagnostics scripts for testing connectivity, analyzing network traffic, and troubleshooting network issues - essential skills for DevOps engineers.


Terminal window
# Common network tools
ip # Modern network configuration
ss # Socket statistics
ping # Test connectivity
traceroute # Trace network path
dig # DNS queries
netstat # Network statistics
tcpdump # Packet capture
nmap # Network scanning

#!/usr/bin/env bash
# ping_test.sh - Test network connectivity
HOST="${1:-google.com}"
COUNT="${2:-4}"
echo "Testing connectivity to $HOST..."
ping -c "$COUNT" "$HOST"
#!/usr/bin/env bash
# monitor_hosts.sh - Monitor multiple hosts
HOSTS=(
"8.8.8.8"
"google.com"
"cloudflare.com"
"1.1.1.1"
)
echo "=== Network Monitoring ==="
for host in "${HOSTS[@]}"; do
if ping -c 1 -W 2 "$host" &>/dev/null; then
echo "$host is reachable"
else
echo "$host is unreachable"
fi
done

#!/usr/bin/env bash
# port_scan.sh - Scan ports on localhost
set -euo pipefail
HOST="${1:-localhost}"
START_PORT="${2:-1}"
END_PORT="${3:-1024}"
echo "Scanning $HOST ports $START_PORT-$END_PORT..."
for port in $(seq $START_PORT $END_PORT); do
if timeout 0.1 bash -c "echo >/dev/tcp/$HOST/$port" 2>/dev/null; then
echo "Port $port: OPEN"
fi
done
#!/usr/bin/env bash
# check_ports.sh - Check if specific ports are open
set -euo pipefail
HOST="${1:-localhost}"
PORTS=(22 80 443 3306 5432 6379 8080)
echo "Checking common ports on $HOST..."
for port in "${PORTS[@]}"; do
if timeout 0.5 bash -c "echo >/dev/tcp/$HOST/$port" 2>/dev/null; then
echo "Port $port: OPEN"
else
echo "Port $port: CLOSED"
fi
done

#!/usr/bin/env bash
# dns_lookup.sh - Perform DNS lookups
set -euo pipefail
DOMAIN="${1:-google.com}"
echo "=== DNS Lookup for $DOMAIN ==="
# A record
echo "A record:"
dig +short "$DOMAIN" A
# AAAA record (IPv6)
echo "AAAA record:"
dig +short "$DOMAIN" AAAA
# MX record
echo "MX record:"
dig +short "$DOMAIN" MX
# NS record
echo "NS record:"
dig +short "$DOMAIN" NS
# TXT record
echo "TXT record:"
dig +short "$DOMAIN" TXT
#!/usr/bin/env bash
# reverse_dns.sh - Reverse DNS lookup
set -euo pipefail
IP="${1:-8.8.8.8}"
echo "Reverse DNS for $IP:"
dig +short -x "$IP"

#!/usr/bin/env bash
# network_stats.sh - Show network statistics
echo "=== Network Statistics ==="
echo ""
echo "--- Established Connections ---"
ss -tun | grep ESTAB
echo ""
echo "--- Listening Ports ---"
ss -tunl | grep LISTEN
echo ""
echo "--- Connection States ---"
ss -tan | awk '{print $1}' | sort | uniq -c | sort -rn
#!/usr/bin/env bash
# interface_stats.sh - Show network interface statistics
set -euo pipefail
INTERFACE="${1:-}"
echo "=== Network Interface Statistics ==="
if [[ -z "$INTERFACE" ]]; then
ip -s link show
else
ip -s link show "$INTERFACE"
fi

#!/usr/bin/env bash
# speed_test.sh - Test network speed
set -euo pipefail
# Test download speed
echo "Testing download speed..."
download_speed=$(curl -s -o /dev/null -w '%{speed_download}' https://speed.cloudflare.com/__down?bytes=10000000)
echo "Download: $((download_speed/1024/1024)) MB/s"
# Test upload speed
echo "Testing upload speed..."
upload_speed=$(curl -s -o /dev/null -w '%{speed_upload}' https://speed.cloudflare.com/__up -X POST -d "test")
echo "Upload: $((upload_speed/1024/1024)) MB/s"

#!/usr/bin/env bash
# troubleshoot_dns.sh - Troubleshoot DNS issues
set -euo pipefail
DOMAIN="${1:-google.com}"
echo "=== DNS Troubleshooting for $DOMAIN ==="
echo ""
# Check if DNS server is reachable
echo "1. Checking DNS server..."
ping -c 1 8.8.8.8 &>/dev/null && echo "✓ DNS server reachable" || echo "✗ DNS server unreachable"
# Test DNS resolution
echo ""
echo "2. Testing DNS resolution..."
if dig +short "$DOMAIN" | grep -q '[0-9]'; then
echo "✓ DNS resolution working"
dig +short "$DOMAIN"
else
echo "✗ DNS resolution failed"
fi
# Test with different DNS servers
echo ""
echo "3. Testing with different DNS servers..."
for dns in "8.8.8.8" "1.1.1.1" "208.67.222.222"; do
result=$(dig +short @"$dns" "$DOMAIN" A | head -1)
echo "DNS $dns: $result"
done
#!/usr/bin/env bash
# trace_route.sh - Trace network path
set -euo pipefail
HOST="${1:-google.com}"
echo "=== Traceroute to $HOST ==="
traceroute -m 15 "$HOST"

#!/usr/bin/env bash
# list_iptables.sh - List iptables rules
echo "=== iptables Rules ==="
echo ""
echo "--- Filter Table ---"
sudo iptables -L -n -v
echo ""
echo "--- NAT Table ---"
sudo iptables -t nat -L -n -v

#!/usr/bin/env bash
# test_endpoint.sh - Test HTTP/HTTPS endpoints
set -euo pipefail
URL="${1:-http://localhost:8080/health}"
echo "Testing: $URL"
echo ""
# HTTP status code
status_code=$(curl -s -o /dev/null -w "%{http_code}" "$URL")
echo "HTTP Status: $status_code"
# Response time
time_total=$(curl -s -o /dev/null -w "%{time_total}" "$URL")
echo "Response Time: ${time_total}s"
# Content type
content_type=$(curl -s -I "$URL" | grep -i "content-type" | cut -d: -f2 | xargs)
echo "Content-Type: $content_type"
# Check SSL certificate (for HTTPS)
if [[ "$URL" =~ ^https ]]; then
echo ""
echo "SSL Certificate:"
echo "$URL" | openssl s_client -connect "${URL#https://}" 2>/dev/null | \
openssl x509 -noout -dates -subject
fi

#!/usr/bin/env bash
# capture_packets.sh - Capture network packets
set -euo pipefail
INTERFACE="${1:-eth0}"
COUNT="${2:-100}"
OUTPUT_FILE="${3:-/tmp/capture.pcap}"
echo "Capturing $COUNT packets on $INTERFACE..."
sudo tcpdump -i "$INTERFACE" -c "$COUNT" -w "$OUTPUT_FILE"
echo "Capture saved to: $OUTPUT_FILE"
#!/usr/bin/env bash
# latency_test.sh - Test network latency
set -euo pipefail
HOST="${1:-google.com}"
COUNT="${2:-10}"
echo "Testing latency to $HOST..."
echo ""
# Measure latency
ping -c "$COUNT" "$HOST" | tail -1
# Show RTT statistics
ping -c "$COUNT" "$HOST" | grep "rtt"

In this chapter, you learned:

  • ✅ Connectivity testing with ping
  • ✅ Port scanning and analysis
  • ✅ DNS lookup and troubleshooting
  • ✅ Network statistics
  • ✅ Bandwidth testing
  • ✅ DNS troubleshooting
  • ✅ Route tracing
  • ✅ Firewall rules
  • ✅ HTTP endpoint testing
  • ✅ Packet capture
  • ✅ Latency analysis

Continue to the next chapter to learn about Container Automation Scripts.


Previous Chapter: Log Analysis Scripts Next Chapter: Container Automation Scripts