Skip to content

Instantly share code, notes, and snippets.

@enkiusz
Forked from ambakshi/vmrun.sh
Created February 14, 2018 16:36
Show Gist options
  • Save enkiusz/b37e9fc57f0337b855624412053e3f9b to your computer and use it in GitHub Desktop.
Save enkiusz/b37e9fc57f0337b855624412053e3f9b to your computer and use it in GitHub Desktop.
vmrun wrapper to run a command over vix and get the stdout/stderr
#!/bin/bash
#
# vmrun.sh
#
# Wrapper around vmrun to make running a shell command
# or script in the path easier. Outputs stdout/stderr
# from the command in the guest and exits with the
# same return code.
#
# Amit Bakshi
# 12/25/2014
case "$(uname -s)" in
Darwin)
VMINSTALL="/Applications/VMware Fusion.app/Contents/Library"
VMPROV="fusion"
;;
Windows)
VMINSTALL="/cygdrive/c/Program Files/VMware Workstation/bin"
VMPROV="ws"
;;
*) echo >&2 "Unknown system $(uname -s)"
exit 2
;;
esac
if [ -z "$VMX" ]; then
VMX="$(find "$PWD" -name '*.vmx' -maxdepth 3 | head -1)"
fi
vmrun () {
cmd="$1"
shift
set -- "$cmd" "$VMX" "$@"
"$VMINSTALL/vmrun" -T "$VMPROV" -gu devp -gp devp "$@"
}
runscript_windows () {
echo '@C:\Windows\System32\cmd.exe /c '"$@"' > C:\Windows\Temp\'$$'.out 2>&1' > /tmp/$$.bat
vmrun CopyFileFromHostToGuest /tmp/$$.bat C:\\Windows\\Temp\\$$.bat || return 1
vmrun runProgramInGuest C:\\Windows\\Temp\\$$.bat
res=$?
vmrun CopyFileFromGuestToHost C:\\Windows\\Temp\\$$.out /tmp/$$.out &&
cat /tmp/$$.out 2>/dev/null
rm -f /tmp/$$.{out,bat}
vmrun deleteFileInGuest C:\\Windows\\Temp\\$$.bat
vmrun deleteFileInGuest C:\\Windows\\Temp\\$$.out
return $res
}
runscript_linux () {
echo -ne '#!/bin/bash\n'"$@"' > /tmp/'$$'.out 2>&1' > /tmp/$$.sh
chmod +x /tmp/$$.sh
vmrun CopyFileFromHostToGuest /tmp/$$.sh /tmp/$$.sh || return 1
vmrun runProgramInGuest /tmp/$$.sh
res=$?
vmrun CopyFileFromGuestToHost /tmp/$$.out /tmp/$$.out &&
cat /tmp/$$.out 2>/dev/null
rm -f /tmp/$$.out
vmrun deleteFileInGuest /tmp/$$.out
vmrun deleteFileInGuest /tmp/$$.sh
return $res
}
GUESTOS="$("$VMINSTALL/vmrun" -T "$VMPROV" readVariable "$VMX" runtimeConfig guestOS)"
case "$GUESTOS" in
windows*) runscript_windows "$@" ;;
rhel*|centos*|linux*) runscript_linux "$@" ;;
*) echo >&2 "Unknown guestOS: $GUESTOS"; exit 2 ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment