|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright (c) 2021-2025 community-scripts ORG |
| 4 | +# Author: jeroenzwart |
| 5 | +# License: MIT |
| 6 | +# https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE |
| 7 | + |
| 8 | +function header_info() { |
| 9 | + clear |
| 10 | + cat <<"EOF" |
| 11 | + ______ __ __ _ ________ |
| 12 | + / ____/ _____ _______ __/ /____ / / | |/ / ____/ |
| 13 | + / __/ | |/_/ _ \/ ___/ / / / __/ _ \ / / | / / |
| 14 | + / /____> </ __/ /__/ /_/ / /_/ __/ / /___/ / /___ |
| 15 | +/_____/_/|_|\___/\___/\__,_/\__/\___/ /_____/_/|_\____/ |
| 16 | + |
| 17 | +EOF |
| 18 | +} |
| 19 | +set -eEuo pipefail |
| 20 | +BL=$(echo "\033[36m") |
| 21 | +RD=$(echo "\033[01;31m") |
| 22 | +CM='\xE2\x9C\x94\033' |
| 23 | +GN=$(echo "\033[1;92m") |
| 24 | +CL=$(echo "\033[m") |
| 25 | +header_info |
| 26 | +echo "Loading..." |
| 27 | +whiptail --backtitle "Proxmox VE Helper Scripts" --title "Proxmox VE LXC Execute" --yesno "This will execute a command inside selected LXC Containers. Proceed?" 10 58 |
| 28 | +NODE=$(hostname) |
| 29 | +EXCLUDE_MENU=() |
| 30 | +MSG_MAX_LENGTH=0 |
| 31 | +while read -r TAG ITEM; do |
| 32 | + OFFSET=2 |
| 33 | + ((${#ITEM} + OFFSET > MSG_MAX_LENGTH)) && MSG_MAX_LENGTH=${#ITEM}+OFFSET |
| 34 | + EXCLUDE_MENU+=("$TAG" "$ITEM " "OFF") |
| 35 | +done < <(pct list | awk 'NR>1') |
| 36 | +excluded_containers=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Containers on $NODE" --checklist "\nSelect containers to skip from executing:\n" \ |
| 37 | + 16 $((MSG_MAX_LENGTH + 23)) 6 "${EXCLUDE_MENU[@]}" 3>&1 1>&2 2>&3 | tr -d '"') |
| 38 | + |
| 39 | +if [ $? -ne 0 ]; then |
| 40 | + exit |
| 41 | +fi |
| 42 | + |
| 43 | + |
| 44 | +read -r -p "Enter here command for inside the containers: " custom_command |
| 45 | + |
| 46 | +header_info |
| 47 | +echo "One moment please...\n" |
| 48 | + |
| 49 | +function execute_in() { |
| 50 | + container=$1 |
| 51 | + name=$(pct exec "$container" hostname) |
| 52 | + echo -e "${BL}[Info]${GN} Execute inside${BL} ${name}${GN} with output: ${CL}" |
| 53 | + pct exec "$container" -- bash -c "${custom_command}" | tee |
| 54 | +} |
| 55 | + |
| 56 | +for container in $(pct list | awk '{if(NR>1) print $1}'); do |
| 57 | + if [[ " ${excluded_containers[@]} " =~ " $container " ]]; then |
| 58 | + echo -e "${BL}[Info]${GN} Skipping ${BL}$container${CL}" |
| 59 | + else |
| 60 | + os=$(pct config "$container" | awk '/^ostype/ {print $2}') |
| 61 | + if [ "$os" != "debian" ] && [ "$os" != "ubuntu" ]; then |
| 62 | + echo -e "${BL}[Info]${GN} Skipping ${name} ${RD}$container is not Debian or Ubuntu ${CL}" |
| 63 | + continue |
| 64 | + fi |
| 65 | + |
| 66 | + status=$(pct status "$container") |
| 67 | + template=$(pct config "$container" | grep -q "template:" && echo "true" || echo "false") |
| 68 | + if [ "$template" == "false" ] && [ "$status" == "status: stopped" ]; then |
| 69 | + echo -e "${BL}[Info]${GN} Starting${BL} $container ${CL}" |
| 70 | + pct start "$container" |
| 71 | + echo -e "${BL}[Info]${GN} Waiting For${BL} $container${CL}${GN} To Start ${CL}" |
| 72 | + sleep 5 |
| 73 | + execute_in "$container" |
| 74 | + echo -e "${BL}[Info]${GN} Shutting down${BL} $container ${CL}" |
| 75 | + pct shutdown "$container" & |
| 76 | + elif [ "$status" == "status: running" ]; then |
| 77 | + execute_in "$container" |
| 78 | + fi |
| 79 | + fi |
| 80 | +done |
| 81 | + |
| 82 | +wait |
| 83 | + |
| 84 | +echo -e "${GN} Finished, execute command inside selected containers. ${CL} \n" |
0 commit comments