Skip to content
TheChatArchiveThe Chat Archive
grok-3-preview-02-24· xAIHugging Face LMArena Human Preference 140K

Write a script that rootlessly creates a temporary wireguard container using a .conf…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Qwenqwen3-30b-a3b’s answer.
  1. User

    Write a script that rootlessly creates a temporary wireguard container using a .conf then runs the user supplied command-line inside.

  2. Assistant

    Here's a script that creates a rootless WireGuard container using Podman (a rootless container engine) to run a user-supplied command through a WireGuard VPN connection. The script uses a provided WireGuard configuration file (.conf) and ensures proper cleanup of resources.

    #!/bin/bash
    
    # Script to create a rootless WireGuard container and run a command inside it
    
    # Exit on any error
    set -e
    
    # Function to display usage
    usage() {
        echo "Usage: $0 <wireguard-config.conf> <command> [args...]"
        echo "Example: $0 /path/to/wg.conf curl https://ifconfig.me"
        exit 1
    }
    
    # Check if Podman is installed
    if ! command -v podman &> /dev/null; then
        echo "Error: Podman is not installed. Please install Podman to use this script."
        exit 1
    fi
    
    # Check if at least two arguments are provided
    if [ $# -lt 2 ]; then
        usage
    fi
    
    # WireGuard config file
    WG_CONF="$1"
    shift
    
    # Check if the config file exists
    if [ ! -f "$WG_CONF" ]; then
        echo "Error: WireGuard config file '$WG_CONF' not found."
        exit 1
    fi
    
    # Command to run inside the container
    USER_COMMAND="$@"
    
    # Temporary container name
    CONTAINER_NAME="wg-temp-$(date +%s)"
    
    # Function to clean up the container
    cleanup() {
        echo "Cleaning up container..."
        podman rm -f "$CONTAINER_NAME" > /dev/null 2>&1
    }
    
    # Trap script exit to ensure cleanup
    trap cleanup EXIT
    
    echo "Starting WireGuard container..."
    
    # Create and start the container
    podman run --rm -it \
        --name "$CONTAINER_NAME" \
        --cap-add=NET_ADMIN \
        --cap-add=NET_RAW \
        --volume "$WG_CONF:/etc/wireguard/wg0.conf:ro" \
        --sysctl net.ipv4.ip_forward=1 \
        --sysctl net.ipv6.conf.all.forwarding=1 \
        docker.io/alpine:latest \
        /bin/sh -c "
            # Install WireGuard tools
            apk add --no-cache wireguard-tools bash iproute2 &&
            # Start WireGuard
            wg-quick up /etc/wireguard/wg0.conf &&
            # Execute the user command
            exec $USER_COMMAND
        "
    
    # The trap will handle cleanup automatically
    

    Explanation of the Script

    1. Shebang and Error Handling:

      • The script starts with #!/bin/bash to indicate it's a Bash script.
      • set -e ensures the script exits immediately if any command fails.
    2. Usage Function:

      • The usage function displays help text if the script is not used correctly.
    3. Dependencies Check:

      • The script checks if Podman is installed, as it is required for rootless container management.
    4. Argument Handling:

      • The first argument ($1) is the path to the WireGuard configuration file (.conf).
      • The remaining arguments ("$@") are the command and its arguments to run inside the container.
    5. Container Setup:

      • A unique container name is generated using a timestamp to avoid conflicts.
      • The container is created using the alpine image for minimal size.
      • Necessary capabilities (NET_ADMIN, NET_RAW) are added to allow WireGuard to manage network interfaces.
      • The WireGuard configuration file is mounted into the container at /etc/wireguard/wg0.conf.
      • Sysctl settings are applied to enable IP forwarding (required for VPN functionality).
    6. Container Execution:

      • Inside the container, the script installs WireGuard tools (wireguard-tools), starts the VPN using wg-quick, and then executes the user-supplied command.
      • The exec ensures the user command replaces the shell process, allowing proper signal handling.
    7. Cleanup:

      • A cleanup function is defined to remove the container when the script exits.
      • The trap command ensures the cleanup function is called on script exit, whether successful or due to an error.

    Prerequisites

    • Podman: Install Podman on your system. On most Linux distributions, you can install it with your package manager (e.g., sudo apt install podman on Debian/Ubuntu or sudo dnf install podman on Fedora).
    • WireGuard Configuration: You need a valid WireGuard .conf file, typically provided by your VPN provider or self-hosted WireGuard server.

    Usage

    1. Save the script as wg-container.sh.
    2. Make it executable:
      chmod +x wg-container.sh
      
    3. Run the script with a WireGuard configuration file and a command:
      ./wg-container.sh /path/to/wg.conf curl https://ifconfig.me
      
      This example will start a WireGuard VPN connection and run curl https://ifconfig.me to display your public IP address through the VPN.

    Notes

    • Rootless Operation: This script uses Podman, which runs containers without root privileges. Ensure your user has the necessary permissions to run Podman (e.g., subuid/subgid mappings are set up).
    • Security: Be cautious with the WireGuard configuration file, as it contains sensitive information (e.g., private keys).
    • Error Handling: The script exits on errors and ensures cleanup, but you may want to add additional error handling depending on your use case.
    • Customization: You can modify the base image (e.g., use ubuntu instead of alpine) or add additional tools inside the container by modifying the apk add command.

    This script provides a convenient way to run commands through a WireGuard VPN without requiring root privileges or persistent container state.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.