diff --git a/bin/pct-list b/bin/pct-list new file mode 100755 index 0000000..66a3ca4 --- /dev/null +++ b/bin/pct-list @@ -0,0 +1,39 @@ +#!/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 + 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" + done +} + +# MAIN + +set -Eue +# [[ "$#" -lt 1 ]] && usage && exit 1 +list diff --git a/bin/pct-lookup b/bin/pct-lookup index a9aff4c..e620f24 100755 --- a/bin/pct-lookup +++ b/bin/pct-lookup @@ -11,20 +11,17 @@ function usage { } function lookup { - cmd="pvesh get /cluster/resources --type vm --output=json | jq --raw-output '.[] | \"\(.name) \(.vmid)\"'" if [[ $CONTAINER_NAME == "--all" ]]; then - eval "$cmd" + pct-list else - cmd="$cmd | egrep '^$CONTAINER_NAME '" - found=$(eval "$cmd") || ( >&2 echo "ERROR: container <$CONTAINER_NAME> not found!" && exit 10 ) - echo "$found" | cut -d' ' -f2 + conf=$(grep -m1 -rl --include="*.conf" "^hostname: $CONTAINER_NAME$" /etc/pve/lxc/) && conf=${conf#/etc/pve/lxc/} && conf=${conf%.conf} + [[ -z $conf ]] && (echo >&2 "ERROR: container <$CONTAINER_NAME> not found!" && exit 10) + echo $conf fi } - # MAIN set -Eue [[ "$#" -lt 1 ]] && usage && exit 1 lookup - diff --git a/bin/pct-rename b/bin/pct-rename index b95bdb5..a9a9dfe 100755 --- a/bin/pct-rename +++ b/bin/pct-rename @@ -15,9 +15,6 @@ function usage { function parse_options { while [[ $# -gt 0 ]]; do case "$1" in - list-bridges | list-zfs-pools | list-dir-pools) - COMMAND=$1 - ;; --help | -h) usage && exit 0 ;;