Posted on
Administration

Creating a Custom Bash Script for Network Diagnostics

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Creating a Custom Bash Script for Network Diagnostics

Network diagnostics are vital for troubleshooting and maintaining system connectivity. Bash scripts can simplify tasks like checking connectivity, diagnosing network issues, and gathering performance metrics. In this guide, we will create a custom Bash script for network diagnostics.


Step 1: Writing a Basic Network Diagnostics Script

Here is a foundational Bash script to perform essential network diagnostic tasks:

#!/bin/bash

# Variables
LOG_FILE="/var/log/network_diagnostics.log"  # Log file for diagnostics
PING_TARGET="8.8.8.8"                      # Default target for connectivity test
INTERFACE="eth0"                          # Network interface to monitor

# Function to check connectivity
check_connectivity() {
  if ping -c 4 "$PING_TARGET" &>/dev/null; then
    echo "[$(date)] INFO: Connectivity to $PING_TARGET is successful." >> "$LOG_FILE"
  else
    echo "[$(date)] ERROR: Unable to reach $PING_TARGET." >> "$LOG_FILE"
  fi
}

# Function to display network interface details
interface_details() {
  ifconfig "$INTERFACE" >> "$LOG_FILE"
  echo "[$(date)] INFO: Network interface $INTERFACE details logged." >> "$LOG_FILE"
}

# Function to check DNS resolution
check_dns() {
  local DOMAIN=$1
  if nslookup "$DOMAIN" &>/dev/null; then
    echo "[$(date)] INFO: DNS resolution for $DOMAIN is successful." >> "$LOG_FILE"
  else
    echo "[$(date)] ERROR: DNS resolution for $DOMAIN failed." >> "$LOG_FILE"
  fi
}

# Function to measure network speed
measure_speed() {
  if command -v speedtest-cli &>/dev/null; then
    speedtest-cli >> "$LOG_FILE"
    echo "[$(date)] INFO: Network speed test completed." >> "$LOG_FILE"
  else
    echo "[$(date)] ERROR: speedtest-cli is not installed." >> "$LOG_FILE"
  fi
}

# Main script
case $1 in
  connectivity)
    check_connectivity
    ;;
  details)
    interface_details
    ;;
  dns)
    check_dns "$2"
    ;;
  speed)
    measure_speed
    ;;
  *)
    echo "Usage: $0 {connectivity|details|dns <domain>|speed}" >> "$LOG_FILE"
    ;;
esac

Step 2: Automating the Script with Cron

To automate network diagnostics, schedule the script using cron. For example, to check connectivity every 5 minutes:

  1. Open the crontab editor:

    crontab -e
    
  2. Add an entry to schedule the connectivity check:

    # Check connectivity every 5 minutes
    */5 * * * * /var/log/network_diagnostics.sh connectivity
    
  3. Save and exit. The task will now execute automatically.


Step 3: Enhancements and Customizations

1. Email Alerts

Send email notifications for connectivity failures:

send_email_alert() {
  local SUBJECT="Network Diagnostics Alert"
  local BODY="$1"
  echo "$BODY" | mailx -s "$SUBJECT" admin@example.com
}

# Call this function in check_connectivity()
if ! ping -c 4 "$PING_TARGET" &>/dev/null; then
  send_email_alert "Connectivity to $PING_TARGET failed."
fi

2. Monitoring Multiple Interfaces

Extend the script to monitor multiple network interfaces:

INTERFACES=("eth0" "wlan0")
for IFACE in "${INTERFACES[@]}"; do
  INTERFACE="$IFACE"
  interface_details
done

3. Customizable Targets

Allow users to specify a custom target for connectivity tests:

PING_TARGET=${2:-"8.8.8.8"}
check_connectivity

4. Integration with Logging Tools

Export network diagnostics logs for analysis in tools like ELK Stack:

export_logs() {
  local ELK_LOG_DIR="/var/log/elk/logs"
  cp "$LOG_FILE" "$ELK_LOG_DIR"
  echo "[$(date)] INFO: Logs exported to $ELK_LOG_DIR." >> "$LOG_FILE"
}

# Call this function periodically to export logs
export_logs

Conclusion

A custom Bash script for network diagnostics can automate common troubleshooting tasks, saving time and improving system reliability. By adding features like email alerts, interface monitoring, and integration with logging tools, you can create a robust solution tailored to your needs. Start building your network diagnostics script today to streamline your network management tasks!