Last active
February 14, 2018 16:36
-
-
Save ambakshi/ff30bfe740797eaff0b8 to your computer and use it in GitHub Desktop.
vmrun wrapper to run a command over vix and get the stdout/stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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