Multi-Cloud Network Performance Monitoring Script 📊

23/01/2025 security 4 mins read
Table Of Contents

Prerequisites

  • Linux/Unix environment with bash shell
  • SSH access to your cloud instances
  • iperf3 installed locally
  • Cloud instance with:
    • Open ports for SSH (22), iperf3 (5201), and ICMP (ping)
    • Public IP address
    • SSH key-based authentication configured

Installation

First, ensure you have iperf3 installed on your local machine:

Terminal window
sudo apt-get update && sudo apt-get install -y iperf3
Terminal window
sudo yum install -y iperf3
Terminal window
brew install iperf3

Script Implementation

Create a new file named cloud-network-monitor.sh:

#!/bin/bash
# Cloud provider configurations
# Uncomment and configure your desired cloud provider
# AWS Configuration
#CLOUD_HOST="[email protected]"
#KEY_PATH="/root/.ssh/aws_key"
# Oracle Cloud Configuration
#CLOUD_HOST="[email protected]" # Oracle instances use the public IP
#KEY_PATH="/root/.ssh/oracle_key"
# DigitalOcean Configuration
#CLOUD_HOST="[email protected]" # DigitalOcean droplets typically use root user
#KEY_PATH="/root/.ssh/digitalocean_key"
# Common Configuration
INTERVAL=60
LOG_FILE="network_metrics.log"
# Function to validate configuration
validate_config() {
if [ -z "$CLOUD_HOST" ] || [ -z "$KEY_PATH" ]; then
echo "Error: Please configure CLOUD_HOST and KEY_PATH variables"
exit 1
fi
if [ ! -f "$KEY_PATH" ]; then
echo "Error: SSH key not found at $KEY_PATH"
exit 1
fi
}
# Function to check network metrics
check_network() {
local cloud_ip=$(echo $CLOUD_HOST | cut -d@ -f2)
echo "=== Network Test $(date) ===" | tee -a $LOG_FILE
echo "Testing connection to: $CLOUD_HOST" | tee -a $LOG_FILE
# Test ping latency (5 packets)
echo "Testing latency..." | tee -a $LOG_FILE
ping -c 5 $cloud_ip | tee -a $LOG_FILE
# Test bandwidth using iperf3
echo "Testing bandwidth..." | tee -a $LOG_FILE
# Start iperf3 server on cloud VM with error handling
if ! ssh -i $KEY_PATH -o StrictHostKeyChecking=accept-new $CLOUD_HOST "which iperf3 || sudo apt-get update && sudo apt-get install -y iperf3"; then
echo "Failed to ensure iperf3 is installed on cloud server" | tee -a $LOG_FILE
return 1
fi
if ! ssh -i $KEY_PATH $CLOUD_HOST "iperf3 -s -D"; then
echo "Failed to start iperf3 server on cloud instance" | tee -a $LOG_FILE
return 1
fi
# Wait for server to start
sleep 2
# Run bandwidth test locally
iperf3 -c $cloud_ip -t 10 | tee -a $LOG_FILE
# Stop iperf3 server on cloud VM
ssh -i $KEY_PATH $CLOUD_HOST "pkill iperf3"
echo "----------------------------------------" | tee -a $LOG_FILE
}
# Validate configuration before starting
validate_config
# Main loop
while true; do
check_network || echo "Network check failed, will retry in $INTERVAL seconds"
sleep $INTERVAL
done

Cloud Provider Setup

AWS Configuration

  1. Configure your security group to allow:

    Type: All ICMP - IPv4
    Source: Your home IP
    Type: Custom TCP
    Port: 5201
    Source: Your home IP
    Type: SSH
    Port: 22
    Source: Your home IP
  2. Ensure your SSH key is in OpenSSH format (if using .ppk):

    puttygen /path/to/key.ppk -O private-openssh -o /root/.ssh/aws_key
    chmod 600 /root/.ssh/aws_key

Oracle Cloud Configuration

  1. Add the following security rules to your VCN:
    TCP port 22 (SSH)
    TCP port 5201 (iperf3)
    ICMP Type 3 (ping)

DigitalOcean Configuration

  1. Create firewall rules in the DigitalOcean console:
    TCP port 22 (SSH)
    TCP port 5201 (iperf3)
    ICMP (ping)

Usage

  1. Set proper permissions for the script:

    chmod +x cloud-network-monitor.sh
  2. Update the configuration section with your cloud instance details

  3. Run the script:

    ./cloud-network-monitor.sh

The script will continuously monitor:

  • Network latency using ping
  • Bandwidth using iperf3
  • Results are logged to network_metrics.log

Troubleshooting

If you encounter SSH key issues:

# Fix key permissions
chmod 600 /path/to/your/key
# Test SSH connection
ssh -i /path/to/your/key -v your_cloud_host

For iperf3 connection issues:

# Test iperf3 manually
iperf3 -c your_cloud_ip -p 5201