#!/bin/bash # Ready-to-use Docker container script for BGP server # BGP Container Runner Script CONTAINER_NAME="bgp-server" IMAGE_NAME="quay.io/frrouting/frr:latest" BGP_PORT="179" VTYSH_PORT="2601" CONFIG_DIR="$(pwd)" echo -e "\033[36m=== BGP Server Container Runner ===\033[0m" # Function to check if Docker is running check_docker() { if ! docker version >/dev/null 2>&1; then echo -e "\033[31mError: Docker is not running. Please start Docker first.\033[0m" exit 1 fi echo -e "\033[32m✓ Docker is running\033[0m" } # Function to stop existing container stop_container() { if docker ps -q -f name=$CONTAINER_NAME | grep -q .; then echo -e "\033[33mStopping existing container...\033[0m" docker stop $CONTAINER_NAME docker rm $CONTAINER_NAME fi } # Function to pull image pull_image() { echo -e "\033[33mPulling FRRouting image...\033[0m" docker pull $IMAGE_NAME } # Function to create and run container run_container() { echo -e "\033[33mStarting BGP container...\033[0m" docker run -d \ --name $CONTAINER_NAME \ --hostname $CONTAINER_NAME \ --privileged \ --network host \ -v "$CONFIG_DIR/frr.conf:/etc/frr/frr.conf:ro" \ -v "$CONFIG_DIR/prefixes.txt:/etc/frr/prefixes.txt:ro" \ -e FRR_USER=frr \ -e FRR_VTYSH_PASSWORD=zebra \ --cap-add=NET_ADMIN \ --cap-add=SYS_ADMIN \ --security-opt seccomp=unconfined \ --restart unless-stopped \ $IMAGE_NAME \ /usr/lib/frr/frrinit.sh start } # Function to check container status check_status() { echo -e "\033[36mContainer Status:\033[0m" docker ps --filter "name=$CONTAINER_NAME" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" echo -e "\n\033[36mNetwork Ports:\033[0m" if netstat -tuln 2>/dev/null | grep -q ":179 "; then echo -e "\033[32m✓ BGP port 179 is listening\033[0m" else echo -e "\033[31m✗ BGP port 179 is not listening\033[0m" fi if netstat -tuln 2>/dev/null | grep -q ":2601 "; then echo -e "\033[32m✓ vtysh port 2601 is listening\033[0m" else echo -e "\033[31m✗ vtysh port 2601 is not listening\033[0m" fi } # Function to show logs show_logs() { echo -e "\n\033[36mContainer Logs:\033[0m" docker logs $CONTAINER_NAME } # Function to show usage show_usage() { echo -e "\033[33mUsage:\033[0m" echo -e " $0 [start|stop|restart|status|logs|help]" echo -e "" echo -e "\033[33mCommands:\033[0m" echo -e " start - Start BGP container" echo -e " stop - Stop BGP container" echo -e " restart - Restart BGP container" echo -e " status - Show container status" echo -e " logs - Show container logs" echo -e " help - Show this help" echo -e "" echo -e "\033[33mExamples:\033[0m" echo -e " $0 start" echo -e " $0 status" echo -e " $0 logs" } # Main script logic case "${1:-start}" in start) check_docker stop_container pull_image run_container sleep 3 check_status show_logs echo -e "\n\033[32mBGP Server is running!\033[0m" echo -e "\033[33mTo connect to vtysh: docker exec -it $CONTAINER_NAME vtysh\033[0m" echo -e "\033[33mTo view logs: $0 logs\033[0m" echo -e "\033[33mTo stop: $0 stop\033[0m" ;; stop) echo -e "\033[33mStopping BGP container...\033[0m" docker stop $CONTAINER_NAME 2>/dev/null || true docker rm $CONTAINER_NAME 2>/dev/null || true echo -e "\033[32mBGP container stopped\033[0m" ;; restart) echo -e "\033[33mRestarting BGP container...\033[0m" $0 stop sleep 2 $0 start ;; status) check_docker check_status ;; logs) if docker ps -q -f name=$CONTAINER_NAME | grep -q .; then show_logs else echo -e "\033[31mContainer is not running\033[0m" fi ;; help|--help|-h) show_usage ;; *) echo -e "\033[31mUnknown command: $1\033[0m" show_usage exit 1 ;; esac