Last active
September 26, 2024 17:33
-
-
Save tdewin/03cf71aa18839732b1171762e2b4a397 to your computer and use it in GitHub Desktop.
GOVC wrapper on Mac, install vmrc for remote console and imagemagick for cropping. Useful mainly for a lab
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/zsh | |
export PATH=$PATH:/Users/$(whoami)/go/bin/ | |
export GOVC_URL=https://192.168. | |
export GOVC_USERNAME=root | |
export GOVC_PASSWORD=Pleaseshareallyourcredentialsongithubnow! | |
export GOVC_INSECURE=true | |
export DEF_SSH_USER=root | |
# cat manage.sh | grep -A100000 "^# githubcopy" | pbcopy | |
# githubcopy | |
# consider | |
# brew install imagemagick | |
# brew install jq | |
COMMON_FIELDS="[.Object.name,.Object.runtime.powerState,.Object.guest.ipAddress,.Object.guest.guestId]" | |
if [[ $1 == "l" || -z $1 ]] | |
then | |
if [[ -z "$1" ]] | |
then | |
echo "(l)ist (d)own (u)p (s)hutdown (r)emoteconsole screensh(o)t (k)eys (c)har (h)ostdown" | |
fi | |
if [[ -z "$2" ]] | |
then | |
govc ls --json=true /ha-datacenter/vm | jq ".elements[] | $COMMON_FIELDS | @csv " -r | column -s, -t | |
else | |
govc ls --json=true /ha-datacenter/vm | jq ".elements[] | select(.Path | match(\"$2\")) | $COMMON_FIELDS | @csv " -r | column -s, -t | |
fi | |
elif [[ $1 == "h" ]] | |
then | |
govc host.shutdown -f=true $(govc ls /ha-datacenter/host) | |
elif [[ $1 == "u" || $1 == "d" || $1 == "r" || $1 == "s" || $1 == "o" || $1 == "k" || $1 == "c" ]] | |
then | |
VM=$(govc ls --json=true /ha-datacenter/vm | jq ".elements[] | select(.Object.name == \"$2\") | .Object") | |
if [[ -z "$VM" ]] | |
then | |
echo "Couldnt find $2" | |
echo "Consider:" | |
govc ls --json=true /ha-datacenter/vm | jq ".elements[] | select(.Path | match(\"$2\")) | $COMMON_FIELDS | @csv " -r | column -s, -t | |
else | |
VMNAME=$(printf '%s' "$VM" | jq -r ".name") | |
VMIP=$(printf '%s' "$VM" | jq -r ".guest.ipAddress") | |
VMGUEST=$(printf '%s' "$VM" | jq -r ".guest.guestId") | |
#echo "found $VMNAME $VMIP $VMGUEST" | |
if [[ $1 == "d" ]] | |
then | |
govc vm.power -off $VMNAME | |
elif [[ $1 == "s" ]] | |
then | |
govc vm.power -s=true $VMNAME | |
elif [[ $1 == "k" ]] | |
then | |
govc vm.keystrokes -vm=$VMNAME -s="${@[3,-1]}" | |
elif [[ $1 == "c" ]] | |
then | |
govc vm.keystrokes -vm=$VMNAME -c=$3 | |
elif [[ $1 == "o" ]] | |
then | |
if [[ -n "$3" && $(expr "$3") -gt 0 ]] | |
then | |
max=$(expr "$3") | |
for x in {1..$max} | |
do | |
printf "%3d/%3d tick\n" $x $max | |
sleep 1 | |
done | |
fi | |
DIROUT=$(date +%y%m%d) | |
mkdir -p $DIROUT | |
PNGOUT=$(date +%y%m%d-%H%M%S-%s)-$VMNAME.png | |
PNG="$DIROUT/$PNGOUT" | |
echo "cheese: $PNG" | |
govc vm.console -capture $PNG $VMNAME | |
[ "$(which magick)" != "magick not found" ] && magick identify -ping -format '{"w":%w,"h":%h}\n' $PNG | |
# $4=-0-42 remove 42 from bottom = windows bar | |
# or with top bar esxi o vm 0 '1024x726+0+42' | |
if [[ -n "$4" ]] | |
then | |
CROP="$DIROUT/CROP-$PNGOUT" | |
magick $PNG -crop $4 $CROP | |
echo "$CROP" | |
else | |
echo "no crop" | |
fi | |
elif [[ $1 == "r" ]] | |
then | |
if [[ $VMIP != "null" ]] | |
then | |
VMWIN=$(printf '%s' "$VM" | jq -r ".guest.guestId | test(\"windows\")") | |
if [[ $VMWIN == "true" ]] | |
then | |
if [[ ! -a "$VMNAME.rdp" ]] | |
then | |
echo "Creating $VMNAME.rdp .." | |
echo "full address:s:$VMIP" > "$VMNAME.rdp" | |
fi | |
echo "Consider open $VMNAME.rdp" | |
else | |
if [[ ! -a "$VMNAME.sh" ]] | |
then | |
echo "Creating $VMNAME.sh .." | |
echo "ssh $VMIP -l $DEF_SSH_USER" > "$VMNAME.sh" | |
chmod +x "$VMNAME.sh" | |
fi | |
echo "Consider ssh with ./$VMNAME.sh" | |
fi | |
else | |
echo "Couldnt find ip, falling over to console" | |
fi | |
open $(govc vm.console $VMNAME) | |
else | |
govc vm.power -on $VMNAME | |
fi | |
fi | |
else | |
govc $@ | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment