一个简单的vbox后台启动/停止/查看虚拟机的脚本

脚本内容如下:

#!/bin/bash

cmd=$1
vm=$2

function info(){
    # 32 green
    ARGE=$1
    echo -e "\033[32mINFO: $ARGE\033[1m"
}

function warn(){
    ARGE=$1
    echo -e "\033[33mWARN: $ARGE\033[1m"
}

function error(){
    ARGE=$1
    echo -e "\033[31mERROR: $ARGE\033[1m"
}

function checkvms(){
    vmlist=$(/usr/bin/VBoxManage list vms|awk -F'"' '{print $2}'|xargs echo|sed 's/ /|/g')
    if [ "${vm}" == "" ];then
		error "请输入vm!"
		exit 1
    elif [[ ! $1 =~ $vmlist ]];then
		error "$1 不存在"
        exit 1
    fi
}

if [[ ! ${cmd} =~ start|ls|lsrun|stop ]];then
	warn "请参考如下命令使用:"
	echo "vmctl [start|ls|lsrun|stop] vm"
	exit 2
fi


case ${cmd} in
start)
     checkvms ${vm}
     /usr/bin/VBoxManage startvm ${vm} --type headless
    ;;
ls)
	/usr/bin/VBoxManage list vms
    ;;
lsrun)
	/usr/bin/VBoxManage list runningvms
    ;;
stop)
	checkvms ${vm}
	/usr/bin/VBoxManage controlvm ${vm} poweroff
    ;;
esac

  

posted @ 2022-01-08 12:12  Linuxbugs  阅读(194)  评论(0编辑  收藏  举报