ljq / vmware-fusion-vmcli-nogui Goto Github PK
View Code? Open in Web Editor NEWvmware fusion shortcut management scripts based on the vmrun cli.
Home Page: https://www.defense.ink
License: MIT License
vmware fusion shortcut management scripts based on the vmrun cli.
Home Page: https://www.defense.ink
License: MIT License
Originally posted by ljq July 20, 2021
Welcome to vmware-fusion-vmcli-nogui Discussions!
You can contact: [email protected]
#!/usr/bin/env bash
#
# Vmware Funsion Operate Linux (Mode: No Gui)
#
# Author: jackliu (Jianqiu Liu)
# Site: https://github.com/ljq
# Blog: https://wdft.com
# Email: [email protected]
# Date: 2020-11-29
# Version: 1.0.0
# script version
CLI_VERSION="1.0.1"
# color green
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced
OK="[\033[1;32m✓\033[0m]"
ERROR="[\033[1;31m✗\033[0m]"
# help info
USEAGE="[usage]: [-l | list] [start|stop|suspend|pause|unpause <number>] [-h | -help | --help] [-v | -V | --version]"
HELP_INFO=$(cat <<EOF
[helptext]
-h|help : Show help info.
-l|list : default[None parameters] show VMs list and VMs status.
start|stop|suspend|pause|unpause [number] :
Start the corresponding virtual machine [Linux based distribution only].
Notice: default show VMs list.
EOF
)
# ---------------------- Functions -------------------------
# fusion vms source path
# etc conf
function etc_ini(){
item=$1
section="BASE"
ini_file="/usr/local/etc/fusion-vmcli.conf"
if [ ! -f "$ini_file" ]; then
return 0
fi
ini_options=`awk -F '=' '/'$section'/{a=1}a==1&&$1~/'${item}'/{print $2;exit}' ${ini_file}`
ini_options=${ini_options//\"/}
echo ${ini_options}
}
# Show list vm nodes
function show_vms(){
local vms=($1)
local total=$2
local num=1
echo -e "${OK} VM List (by Linux distribution)"
for dirname in ${vms[@]}
do
#filter Win
dirname=${dirname//.vmwarevm/}
if [[ "${dirname}" =~ "Win" ]]; then
continue
else
# escape filename
esc_dirname=${dirname//#/\\ }
echo -e " [${num}] ${esc_dirname}"
((num=num+1))
fi
done
echo -e "${OK} By linux distribution total:${total} vm nodes."
}
# get vm total number
function vm_nodes_total(){
local vms=($1)
local total=0
for dirname in ${vms[@]}
do
#filter Win
if [[ "${dirname}" =~ "Win" ]]; then
continue
else
((total=total+1))
fi
done
echo $total
}
# Task exec
function task_exec(){
# check params
local operate=$1
local serial_num=$2
local vms=($3)
local vm_path=$4
local list_total=$5
if [[ -z $operate || -z $serial_num ]]; then
echo -e "${ERROR} missing parameter: operate or serial_num."
exit;
fi
local vmrun_params=("start" "stop" "suspend" "pause" "unpause")
if [[ ${vmrun_params[@]/${operate}/} == ${vmrun_params[@]} ]]; then
echo -e "${ERROR} input invalid parameter: operate."
exit
fi
if [[ $serial_num -le 0 || $serial_num -gt ${list_total} ]]; then
echo -e "${ERROR} serial_num is not range (1-${list_total})"
exit
fi
#vm nodes
local vm_nodes=()
local num=1
for dirname in ${vms[@]}
do
#filter Win
dirname=${dirname//.vmwarevm/}
if [[ "${dirname}" =~ "Win" ]]; then
continue
else
# escape filename
esc_dirname=${dirname//#/\\ }
vm_nodes[$num]=$esc_dirname
((num=num+1))
fi
done
# select node machine
local vm_num_machine=${vm_nodes[${serial_num}]}
echo -e "${OK} VM machine (${vm_num_machine}) $operate ..."
# task exec
#echo "$(which vmrun) $operate $vm_path/$vm_num_machine.vmwarevm/$vm_num_machine.vmx nogui"
output="$("$(which vmrun)" $operate "${vm_path}/${vm_num_machine}.vmwarevm/${vm_num_machine}.vmx" nogui)"
if [ -n "$output" ]; then
echo -e "[\033[1;33m~\033[0m] ${output}"
fi
echo -e "${OK} VM machine(${vm_num_machine}) $operate Finished."
}
# Show vm nodes running
function vms_runing(){
echo -e "${OK} VM Runing List"
printf "$("$(which vmrun)" list | sed "s/^/ [\\\033[1;33m~\\\033[0m] /")"
echo ""
}
# ---------------------- Task Process -------------------------
# check vmware fusion installed status
if which vmrun >/dev/null 2>&1; then
vmrun_version=$(vmrun | grep "vmrun version")
echo -e "${OK} ${vmrun_version}."
else
echo -e "${ERROR} vmrun is not installd."
fi
# help
case $1 in
"-v"|"-V"|"--version")
echo -e "cli script version:${CLI_VERSION}."
exit
;;
"-h"|"-help"|"--help")
echo -e "${HELP_INFO}"
exit
;;
"-l"|"list")
;;
esac
# virtual machines path config (custom)
vm_path="${HOME}/Virtual Machines.localized"
# list dir and process filename space
list_vms=($(ls "${vm_path}" | tr " " "\#"))
list_total=$(vm_nodes_total "${list_vms[*]}")
# vmrun params exec
if [[ ! -z $1 && ! -z $2 ]]; then
# direct exec
task_exec $1 $2 "${list_vms[*]}" "$vm_path" $list_total
vms_runing
else
# show list
show_vms "${list_vms[*]}" $list_total
vms_runing
# interactive exec
printf "${OK} (start|stop|suspend|pause|unpause) num: \033[1;32m"
read operate serial_num
printf "\033[0m"
# cli interactive
task_exec $operate $serial_num "${list_vms[*]}" "$vm_path" $list_total
vms_runing
fi
echo -e "${OK} Task process is done."
exit
➜ ~ fusion-vmcli stop 1
[✓] vmrun version 1.17.0 build-23298085.
[✓] VM machine (OpenWrt) stop ...
[~] Error: The virtual machine is not powered on: /Users/xr/Virtual Machines.localized/OpenWrt.vmwarevm/OpenWrt.vmx
[✓] VM machine(OpenWrt) stop Finished.
[✓] VM Runing List
[~] Total running VMs: 0
[✓] Task process is done.
➜ ~ fusion-vmcli start 1
[✓] vmrun version 1.17.0 build-23298085.
[✓] VM machine (OpenWrt) start ...
[~] 2024-03-22T17:37:53.897| ServiceImpl_Opener: PID 31617
[✓] VM machine(OpenWrt) start Finished.
[✓] VM Runing List
[~] Total running VMs: 1
[~] /Users/xr/Virtual Machines.localized/OpenWrt.vmwarevm/OpenWrt.vmx
[✓] Task process is done.
➜ ~ fusion-vmcli
[✓] vmrun version 1.17.0 build-23298085.
[✓] VM List (by Linux distribution)
[1] OpenWrt
[✓] By linux distribution total:1 vm nodes.
[✓] VM Runing List
[~] Total running VMs: 1
[~] /Users/xr/Virtual Machines.localized/OpenWrt.vmwarevm/OpenWrt.vmx
[✓] (start|stop|suspend|pause|unpause) num: stop 1
[✓] VM machine (OpenWrt) stop ...
[✓] VM machine(OpenWrt) stop Finished.
[✓] VM Runing List
[~] Total running VMs: 0
[✓] Task process is done.
➜ ~ fusion-vmcli -l
[✓] vmrun version 1.17.0 build-23298085.
[✓] VM List (by Linux distribution)
[1] OpenWrt
[✓] By linux distribution total:1 vm nodes.
[✓] VM Runing List
[~] Total running VMs: 0
[✓] (start|stop|suspend|pause|unpause) num: start 1
[✓] VM machine (OpenWrt) start ...
[~] 2024-03-22T17:38:09.409| ServiceImpl_Opener: PID 31785
[✓] VM machine(OpenWrt) start Finished.
[✓] VM Runing List
[~] Total running VMs: 1
[~] /Users/xr/Virtual Machines.localized/OpenWrt.vmwarevm/OpenWrt.vmx
[✓] Task process is done.
➜ ~
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.