mikelieman icon

Attach to Podman container.

mikelieman | PRO | 11/28/20 10:51:58 AM UTC | 0 ⭐ | 1610 👁️ | Never ⏰ | []
Bash |

1.3 KB

|

None

|

0 👍

/

0 👎

#!/usr/bin/bash
 
# Dependencies:  Dialog.  eg 'dnf install dialog'
 
AWK_EXE=$(/usr/bin/which awk)
DIALOG_EXE=$(/usr/bin/which dialog)
GREP_EXE=$(/usr/bin/which grep)
HOSTNAME_EXE=$(/usr/bin/which hostname)
PODMAN_EXE=$(/usr/bin/which podman)
SORT_EXE=$(/usr/bin/which sort)
SUDO_EXE=$(/usr/bin/which sudo)
 
# get a formatted container/image list for the dialog box
CONTAINERS=$( ${SUDO_EXE} ${PODMAN_EXE} ps \
                | ${GREP_EXE} -v "CONTAINER ID" \
                | ${AWK_EXE} '{ printf ("%s %s\n", $12 , $2)}' \
                | ${SORT_EXE} \
                | ${AWK_EXE} '{printf ("%s %s ", $1, $2)}'
            )
 
exec 3>&1
ID=$( ${DIALOG_EXE} --title "Attach to a container" \
        --menu "Choose a container" 19 60 10 $CONTAINERS \
        2>&1 1>&3 
    )
retval=$?
exec 3>&-
 
THIS_HOST=$(${HOSTNAME_EXE} -s)
 
if [ $retval -eq 0 ]; then
    clear
    echo -e "------------------------------------------------------------------------------"
    echo "Spawning new shell in ${ID} ('exit' or ^d to return to ${THIS_HOST})."
    echo -e "------------------------------------------------------------------------------\n"
    ${SUDO_EXE} ${PODMAN_EXE} exec -it ${ID} bash
    echo -e "\n------------------------------------------------------------------------------"
    echo -e "Welcome back to ${THIS_HOST}"
    echo -e "------------------------------------------------------------------------------\n"
fi
 

Comments