#!/bin/bash ############################################################# #### iperf3 Server Setup #### #### Install and configure iperf3 as a systemd service #### #### #### #### Author: Phil Connor #### #### Contact: contact@mylinux.work #### #### License: MIT #### #### Version: 1.0 #### #### #### #### Usage: sudo ./setup-iperf3-server.sh [OPTIONS] #### ############################################################# set -euo pipefail # Default configuration LISTEN_PORT=9182 HARDENED=false UNINSTALL=false SERVICE_NAME="iperf3-server" SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service" show_help() { cat </dev/null 2>&1; then echo "iperf3 is already installed." return fi echo "Installing iperf3..." if command -v apt-get >/dev/null 2>&1; then apt-get update && apt-get install -y iperf3 elif command -v dnf >/dev/null 2>&1; then dnf install -y iperf3 elif command -v yum >/dev/null 2>&1; then yum install -y iperf3 else echo "ERROR: Cannot install iperf3 automatically. Please install manually." exit 1 fi } install_service() { echo "Installing systemd service..." if [[ "$HARDENED" == true ]]; then echo "Using hardened service configuration (private networks only)." cat > "$SERVICE_FILE" < "$SERVICE_FILE" </dev/null | awk '{print $1}') -p ${LISTEN_PORT} -t 10" echo "" echo "To customize settings, edit:" echo " ${SERVICE_FILE}" echo "Then run: sudo systemctl daemon-reload && sudo systemctl restart ${SERVICE_NAME}" } uninstall_service() { echo "Removing iperf3 server service..." systemctl stop "${SERVICE_NAME}" 2>/dev/null || true systemctl disable "${SERVICE_NAME}" 2>/dev/null || true rm -f "$SERVICE_FILE" systemctl daemon-reload echo "iperf3 server service removed." } # --- Main execution --- parse_args "$@" if [[ "$UNINSTALL" == true ]]; then uninstall_service else echo "Setting up iperf3 server service on port ${LISTEN_PORT}..." install_iperf3 install_service fi