mikelieman

Attach to Podman container.

Nov 28th, 2020
893
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/bash
  2.  
  3. # Dependencies:  Dialog.  eg 'dnf install dialog'
  4.  
  5. AWK_EXE=$(/usr/bin/which awk)
  6. DIALOG_EXE=$(/usr/bin/which dialog)
  7. GREP_EXE=$(/usr/bin/which grep)
  8. HOSTNAME_EXE=$(/usr/bin/which hostname)
  9. PODMAN_EXE=$(/usr/bin/which podman)
  10. SORT_EXE=$(/usr/bin/which sort)
  11. SUDO_EXE=$(/usr/bin/which sudo)
  12.  
  13. # get a formatted container/image list for the dialog box
  14. CONTAINERS=$( ${SUDO_EXE} ${PODMAN_EXE} ps \
  15.                 | ${GREP_EXE} -v "CONTAINER ID" \
  16.                 | ${AWK_EXE} '{ printf ("%s %s\n", $12 , $2)}' \
  17.                 | ${SORT_EXE} \
  18.                 | ${AWK_EXE} '{printf ("%s %s ", $1, $2)}'
  19.             )
  20.  
  21. exec 3>&1
  22. ID=$( ${DIALOG_EXE} --title "Attach to a container" \
  23.         --menu "Choose a container" 19 60 10 $CONTAINERS \
  24.         2>&1 1>&3
  25.     )
  26. retval=$?
  27. exec 3>&-
  28.  
  29. THIS_HOST=$(${HOSTNAME_EXE} -s)
  30.  
  31. if [ $retval -eq 0 ]; then
  32.     clear
  33.     echo -e "------------------------------------------------------------------------------"
  34.     echo "Spawning new shell in ${ID} ('exit' or ^d to return to ${THIS_HOST})."
  35.     echo -e "------------------------------------------------------------------------------\n"
  36.     ${SUDO_EXE} ${PODMAN_EXE} exec -it ${ID} bash
  37.     echo -e "\n------------------------------------------------------------------------------"
  38.     echo -e "Welcome back to ${THIS_HOST}"
  39.     echo -e "------------------------------------------------------------------------------\n"
  40. fi
  41.  
RAW Paste Data