ak2766 icon

Fancier LXC listing output

ak2766 | PRO | 02/27/17 11:14:37 PM UTC | 0 ⭐ | 301 👁️ | Never ⏰ | []
Bash |

1.86 KB

|

None

|

0 👍

/

0 👎

#!/bin/bash
 
# colors
RED="^[[1;31m"
GREEN="^[[1;32m"
non="^[[0m"
 
# script to produce fancylxc output
printout="Name State IPv4 Autostart OS­Type"
printout="${printout}"$'\n'"----- ------ ----- ---------- ---------"
cpath="/lxc"
 
me=$(basename $0)
runningonly=0
if [[ ${me} =~ "run" ]]; then
  runningonly=1
fi
 
# check auto start status
chkstart() {
  autostart=$(egrep 'lxc.start.auto[ =]*1' ${cpath}/${name}/config | awk -F= '{print $2}')
  if [[ "${autostart}" == "" ]] || [[ ${autostart} -eq 0 ]]; then
    autostart="NO"
  else
    autostart="YES"
  fi
}
 
chkostype() {
  os=$(egrep -o '[a-z]+.common.conf' ${cpath}/${name}/config)
  case ${os} in
    ubuntu*) ostype="Ubuntu";;
    redhat*) ostype="Redhat";;
    debian*) ostype="Debian";;
    centos*) ostype="CentOS";;
    *) ostype="Unknown";;
  esac
}
 
for cname in $(lxc-ls 2>/dev/null); do
  name="${cname}"
  if [[ "x${cname}" == "x." ]]; then continue; fi
  state=$(lxc-info -n "${cname}" | egrep "^State" | awk -F: '{print $2}')
  if [[ "${state}" =~ "RUNNING" ]]; then
    ipaddr=$(lxc-info -n "${cname}" | egrep "^IP" | awk -F: '{print $2}')
    if [[ "x${ipaddr}" == "x" ]]; then ipaddr="-"; fi
  else
    lxcconfigpath="${cpath}/${cname}/config"
    ipaddr=$(egrep 'ipv4 ?= ?.*$' ${lxcconfigpath} |awk '{print $NF}'|sed 's@/.*$@@')
    if [[ "x${ipaddr}" == "x" ]]; then ipaddr="-"; fi
  fi
  if [[ -r "${cpath}/${cname}/config" ]]; then
    chkstart
    chkostype
  fi
  printout="${printout}"$'\n'"${name} ${state} ${ipaddr} ${autostart} ${ostype}"
done
if [[ ${runningonly} -eq 1 ]]; then
  echo -e "${printout}" | column -t | sed "s/\(STOPPED\)/${RED}\1${non}/i; s/\(RUNNING\)/${GREEN}\1${non}/i" | awk 'NR<3{print;next}{print | "egrep RUNNING"}'
else
  echo -e "${printout}" | column -t | sed "s/\(STOPPED\)/${RED}\1${non}/i; s/\(RUNNING\)/${GREEN}\1${non}/i"
fi

Comments