ak2766

Fancier LXC listing output

Feb 27th, 2017
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.86 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # colors
  4. RED="^[[1;31m"
  5. GREEN="^[[1;32m"
  6. non="^[[0m"
  7.  
  8. # script to produce fancylxc output
  9. printout="Name State IPv4 Autostart OS­Type"
  10. printout="${printout}"$'\n'"----- ------ ----- ---------- ---------"
  11. cpath="/lxc"
  12.  
  13. me=$(basename $0)
  14. runningonly=0
  15. if [[ ${me} =~ "run" ]]; then
  16.   runningonly=1
  17. fi
  18.  
  19. # check auto start status
  20. chkstart() {
  21.   autostart=$(egrep 'lxc.start.auto[ =]*1' ${cpath}/${name}/config | awk -F= '{print $2}')
  22.   if [[ "${autostart}" == "" ]] || [[ ${autostart} -eq 0 ]]; then
  23.     autostart="NO"
  24.   else
  25.     autostart="YES"
  26.   fi
  27. }
  28.  
  29. chkostype() {
  30.   os=$(egrep -o '[a-z]+.common.conf' ${cpath}/${name}/config)
  31.   case ${os} in
  32.     ubuntu*) ostype="Ubuntu";;
  33.     redhat*) ostype="Redhat";;
  34.     debian*) ostype="Debian";;
  35.     centos*) ostype="CentOS";;
  36.     *) ostype="Unknown";;
  37.   esac
  38. }
  39.  
  40. for cname in $(lxc-ls 2>/dev/null); do
  41.   name="${cname}"
  42.   if [[ "x${cname}" == "x." ]]; then continue; fi
  43.   state=$(lxc-info -n "${cname}" | egrep "^State" | awk -F: '{print $2}')
  44.   if [[ "${state}" =~ "RUNNING" ]]; then
  45.     ipaddr=$(lxc-info -n "${cname}" | egrep "^IP" | awk -F: '{print $2}')
  46.     if [[ "x${ipaddr}" == "x" ]]; then ipaddr="-"; fi
  47.   else
  48.     lxcconfigpath="${cpath}/${cname}/config"
  49.     ipaddr=$(egrep 'ipv4 ?= ?.*$' ${lxcconfigpath} |awk '{print $NF}'|sed 's@/.*$@@')
  50.     if [[ "x${ipaddr}" == "x" ]]; then ipaddr="-"; fi
  51.   fi
  52.   if [[ -r "${cpath}/${cname}/config" ]]; then
  53.     chkstart
  54.     chkostype
  55.   fi
  56.   printout="${printout}"$'\n'"${name} ${state} ${ipaddr} ${autostart} ${ostype}"
  57. done
  58. if [[ ${runningonly} -eq 1 ]]; then
  59.   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"}'
  60. else
  61.   echo -e "${printout}" | column -t | sed "s/\(STOPPED\)/${RED}\1${non}/i; s/\(RUNNING\)/${GREEN}\1${non}/i"
  62. fi
Advertisement
Add Comment
Please, Sign In to add comment