You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.3 KiB
59 lines
1.3 KiB
#!/usr/bin/env bash
|
|
|
|
# CONSTANTS
|
|
|
|
CONTAINER_NAME=$1
|
|
|
|
# FUNCTIONS
|
|
|
|
function usage {
|
|
echo "$(basename "$0") [CONTAINER_NAME]"
|
|
}
|
|
|
|
function lookup {
|
|
cmd="pvesh get /cluster/resources --type vm --output=json | jq --raw-output '.[] | \"\(.name) \(.vmid)\"'"
|
|
if [[ $CONTAINER_NAME == "--all" ]]; then
|
|
eval "$cmd"
|
|
else
|
|
cmd="$cmd | egrep '^$CONTAINER_NAME '"
|
|
found=$(eval "$cmd") || (echo >&2 "ERROR: container <$CONTAINER_NAME> not found!" && exit 10)
|
|
echo "$found" | cut -d' ' -f2
|
|
fi
|
|
}
|
|
|
|
function list {
|
|
containers=$(lxc-ls "$CONTAINER_NAME")
|
|
for id in $containers; do
|
|
hostname=$(grep -m1 hostname: "/etc/pve/lxc/$id.conf" | cut -d' ' -f2)
|
|
|
|
info=$(lxc-info -si $id)
|
|
state=($(lxc-info "$id" | grep ^State:)) && state="${state[1]}" # get an array, then take the second element
|
|
|
|
ips_input=($(echo "$info" | grep '^IP:')) || ips_input=()
|
|
ips_result=()
|
|
[[ ${#ips_input[@]} -gt 1 ]] && transform_array_to_odd_values_only ips_input ips_result
|
|
|
|
# convert array to string joined by ','
|
|
ips_result=$(
|
|
IFS=","
|
|
builtin echo "${ips_result[*]}"
|
|
)
|
|
|
|
printf '%-5s %-30s %s %s\n' "$id" "$hostname" "$state" "$ips_result"
|
|
done
|
|
}
|
|
|
|
function transform_array_to_odd_values_only {
|
|
local -n input=$1
|
|
local -n output=$2
|
|
|
|
for i in "${!input[@]}"; do
|
|
((i % 2 == 1)) && output+=("${input[$i]}")
|
|
done
|
|
}
|
|
|
|
# MAIN
|
|
|
|
set -Eue
|
|
# [[ "$#" -lt 1 ]] && usage && exit 1
|
|
list
|