Getting more information from command line.

2 posts / 0 new
Last post
#1 Fri, 10/05/2012 - 10:36
AlReece45

Getting more information from command line.

I'm currently writing a script that runs periodically. I wanted to get some information from virtualmin (domain, username, home). Maybe I'm missing something, but this prooves to be more difficult than I expected using the command line API.

  • The list-domains command truncates the domain (perhaps more).
  • Using the --only-* options works, but adds a considerable delay for each domain (about .72-.91 seconds). It seems like far too much time for simple information (even using the --simple-multiline option)
  • Even using the multiline option, its rather hard to pull the information into a variable (because its formatted for human output, cutting it via ":" is not enough because of the additional space)

I came up with the following bash script to pull pull the information with one virtualmin query, is there a simpler way to do this?

#!/bin/bash
virtualmin_domains=$(virtualmin list-domains --simple-multiline)
while [[ $(echo -n "$virtualmin_domains" | wc -l) -gt 1 ]]; do
  # First line is domain
  domain=$(echo "$virtualmin_domains" | head -n1)
  # Trim the domain name off the input
  virtualmin_domains=$(echo "$virtualmin_domains" | tail -n+2)
  # Find where the indentation ends (where the next domain starts)
  offset_search=$(echo "$virtualmin_domains" | grep -vn "^    ")
  # If the search fails, this is the last domain
  if [ $? -eq 0 ]; then
    # Extract the domain info to process
    offset=$(echo "$offset_search" | head -n1 | cut -d':' -f1)
    info=$(echo "$virtualmin_domains" | head -n$offset)
    # Trim the information off the input
    virtualmin_domains=$(echo "$virtualmin_domains" | tail -n+$offset)
  else
    # This is the last domain. Only the domain info is left
    info="$virtualmin_domains"
    virtualmin_domains=""
  fi
  # Process the domain information
  home=$(echo "$info" | grep "Home directory:" | cut -d' ' -f7)
  user=$(echo "$info" | grep "Username:" | cut -d' ' -f6)
  echo "$domain $home $user"     
done
Fri, 10/05/2012 - 23:10
JamieCameron

If you just want a list of domain names for use in a script, try the command :

virtualmin list-domains --name-only

''

Topic locked