David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 1 | #!/bin/ash |
| 2 | |
| 3 | USERNAME=$1; shift |
| 4 | HOSTIP=$1; shift |
| 5 | SSH_KEY="-i /etc/maas/virtualbox/id_rsa" |
| 6 | |
| 7 | LIST=$(echo $* | awk '{print toupper($0)}') |
| 8 | MATCH= |
| 9 | |
| 10 | best() { |
| 11 | local i |
| 12 | local BEST= |
| 13 | local BEST_CNT=0 |
| 14 | for i in $MATCH; do |
| 15 | local CNT=$(echo $i | cut -d- -f2) |
| 16 | if [ $CNT > $BEST_CNT ]; then |
| 17 | BEST=$(echo $i | cut -d- -f1) |
| 18 | BEST_CNT=$CNT |
| 19 | fi |
| 20 | done |
| 21 | echo $BEST |
| 22 | } |
| 23 | |
| 24 | inc() { |
| 25 | FOUND=$(echo $MATCH | grep "$1" | wc -l) |
| 26 | if [ $FOUND -eq 0 ]; then |
| 27 | MATCH="$MATCH $1-1" |
| 28 | else |
| 29 | NEXT= |
| 30 | local i= |
| 31 | for i in $MATCH; do |
| 32 | FOUND=$(echo $i | grep "$1" | wc -l) |
| 33 | if [ $FOUND -eq 1 ]; then |
| 34 | COUNT=$(echo $i | cut -d- -f2) |
| 35 | COUNT=$(expr $COUNT + 1) |
| 36 | NEXT="$NEXT $1-$COUNT" |
| 37 | else |
| 38 | NEXT="$NEXT $i" |
| 39 | fi |
| 40 | done |
| 41 | MATCH=$NEXT |
| 42 | fi |
| 43 | } |
| 44 | |
| 45 | for i in $(ssh $SSH_KEY $USERNAME@$HOSTIP /usr/local/bin/vboxmanage list vms | awk '{print $NF}' | sed -e 's/[{}]//g'); do |
| 46 | for m in $(ssh $SSH_KEY $USERNAME@$HOSTIP /usr/local/bin/vboxmanage showvminfo --machinereadable $i | grep -i macaddress | cut -d= -f2 | sed -e 's/"//g' -e 's/\(..\)/\1:/g' -e 's/:$//g'); do |
| 47 | p=$(echo $i | cut -d- -f5) |
| 48 | for t in $LIST; do |
| 49 | if [ "$t" == "$m" ]; then |
| 50 | inc $p |
| 51 | fi |
| 52 | done |
| 53 | done |
| 54 | done |
| 55 | |
| 56 | BEST=$(best) |
| 57 | if [ "$BEST x" == " x" ]; then |
| 58 | echo "{}" |
| 59 | else |
| 60 | MAC=$(echo $BEST | cut -d- -f5 | sed -e 's/"//g' -e 's/\(..\)/\1:/g' -e 's/:$//g') |
| 61 | echo "{\"name\":\"amt\",\"mac_address\":\"$MAC\",\"power_password\":\"$USERNAME\",\"power_address\":\"$HOSTIP\"}" |
| 62 | fi |