Tags
#!/bin/bash
# =============================================================================
# Script Name: lxc_list_login.sh
# Version: 03
# Description: Lists LXC containers, checks their statuses, and allows login.
# =============================================================================

# Required commands
REQUIRED_CMDS=("lxc-ls" "lxc-info" "lxc-start" "lxc-attach")

# Check if required commands are available
for CMD in "${REQUIRED_CMDS[@]}"; do
    if ! command -v "$CMD" &> /dev/null; then
        echo "The command $CMD is not installed. Please install it and try again."
        exit 1
    fi
done

# List and check LXC containers
echo "List of all LXC containers:"
CONTAINERS=($(lxc-ls -f | awk 'NR>1 && $1 != "" {print $1}'))

# Check if there are any containers
if [[ ${#CONTAINERS[@]} -eq 0 ]]; then
    echo "There are no LXC containers."
    exit 1
fi

# Display containers and their status
printf "\n%-5s %-20s %-10s\n" "Index" "Container Name" "Status"
echo "------------------------------------------"
for (( I=0; I<${#CONTAINERS[@]}; I++ )); do
    LXCHOSTNAME="${CONTAINERS[$I]}"
    if [[ -n "$LXCHOSTNAME" ]]; then
        STATUS=$(lxc-info --name="$LXCHOSTNAME" | grep "State" | awk '{print $2}')
        printf "%-5d %-20s %-10s\n" "$I" "$LXCHOSTNAME" "$STATUS"
    fi
done

# Prompt user to select a container
read -p "Select a container to log in (0-$(( ${#CONTAINERS[@]} - 1 ))): " SELECTION

# Validate selection
if [[ $SELECTION =~ ^[0-9]+$ ]] && [[ $SELECTION -ge 0 && $SELECTION -lt ${#CONTAINERS[@]} ]]; then
    LXCHOSTNAME="${CONTAINERS[$SELECTION]}"
    STATUS=$(lxc-info --name="$LXCHOSTNAME" | grep "State" | awk '{print $2}')
    
    if [[ $STATUS == "STOPPED" ]]; then
        read -p "Container $LXCHOSTNAME is stopped. Do you want to start it? (y/n) " START_SELECTION
        if [[ $START_SELECTION == "y" ]]; then
            echo "Starting the container $LXCHOSTNAME..."
            if lxc-start --name="$LXCHOSTNAME"; then
                echo "Container $LXCHOSTNAME has been started."
                for i in {1..10}; do
                    STATUS=$(lxc-info --name="$LXCHOSTNAME" | grep "State" | awk '{print $2}')
                    if [[ $STATUS == "RUNNING" ]]; then
                        break
                    fi
                    sleep 1
                done
                if [[ $STATUS != "RUNNING" ]]; then
                    echo "Container $LXCHOSTNAME failed to start within the timeout period."
                    exit 1
                fi
            else
                echo "Error starting the container $LXCHOSTNAME."
                exit 1
            fi
        else
            echo "Container $LXCHOSTNAME was not started."
            exit 1
        fi
    fi
    echo "Logging into the container $LXCHOSTNAME..."
    if ! lxc-attach --name="$LXCHOSTNAME"; then
        echo "Error logging into the container $LXCHOSTNAME."
        exit 1
    fi
else
    echo "Invalid selection. Please run the script again and choose a valid number."
    exit 1
fi

lxc-list-login

vim ~/.zshrc 

alias lxc-list-login="scripts/lxc_list_login.sh"