Browse Source

pct-list multi ips

main
pvincent 2 weeks ago
parent
commit
4c71b2c2f1
  1. 28
      bin/pct-list

28
bin/pct-list

@ -24,11 +24,31 @@ function lookup {
function list {
containers=$(lxc-ls "$CONTAINER_NAME")
for id in $containers; do
info=$(lxc-info -si $id)
state=$(echo "$info" | grep '^State:') && state="${state:15}" # remove 15 leading chars
hostname=$(grep -m1 hostname: "/etc/pve/lxc/$id.conf" | cut -d' ' -f2)
ip=$(echo "$info" | grep '^IP:') && ip="${ip:15}"
printf '%-5s %-30s %s %s\n' "$id" "$hostname" "$state" "$ip"
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
}

Loading…
Cancel
Save