HarshCasper / Rotten-Scripts

Scripts that will make you go WOW!
MIT License
1.48k stars 489 forks source link

Vcentercli #1408

Open sherpaurgen opened 2 years ago

sherpaurgen commented 2 years ago

Description

A simple go client for using vcenter/vmware REST api which can help you if you need to turn on/off multiple machines + check machines power status.

You need to have the api username and password to use this tool

Sample user/pass json file

cat ~/.vmwarepass.json
{
"host":"host1.virtualdc.local",
"username":"staff@virtualdc.com",
"secret":"wli#Lol-2Gbv#"
}

First clone the repo and run build

cd into /path/to/repo

go build

Usage -help :

./vcenterapi -h   //display help
2022/03/01 14:05:36 Connection successful vsph.virtualdc.com:443
Usage of ./vcenterapi:
-list
Lists available virtual machines
-start
start vm700 vm701 #starts vms with vmid vm700 vm701
-stop
stop vm10 vm31 #stops vms with vmid vm10 vm31

List vm's & get the vmid eg.

Output order - vmid , vmName,PoweredState , Memory(MB) , Num of cpu

./vcenterapi -list | grep "192.45.9.19" 

vm-236,Normal_Windows_192.45.9.191,POWERED_OFF,mem:32768,cpu:16

vm-138,Normal_Windows_192.45.9.192,POWERED_OFF,POWERED_OFF,mem:32168,cpu:18

vm-182,Normal_Windows_192.45.9.193,POWERED_OFF,POWERED_ON,mem:4096,cpu:8

Start vm's

./vcenterapi -start vm-24949 vm-51499 vm-51500 vm-51501 vm-51502 vm-51503 vm-5646 vm-69521
2022/03/01 20:42:36 Connection was successful :vsph.virtualdc.com:443
2022/03/01 20:42:36 [vm-24949 vm-51499 vm-51500 vm-51501 vm-51502 vm-51503 vm-5646 vm-69521]
2022/03/01 20:42:39 &{0x1120700 {0xc000114ab0} 0x11d0ee0} 204
2022/03/01 20:42:39 Machine/s started successfully.
2022/03/01 20:42:39 &{0x1120700 {0xc000114ab0} 0x11d0ee0} 204
2022/03/01 20:42:39 Machine/s started successfully.
2022/03/01 20:42:39 &{0x1120700 {0xc000240f20} 0x11d0ee0} 400
2022/03/01 20:42:39 Problem starting vm-5646, already in poweredOn state.
2022/03/01 20:42:40 &{0x1120700 {0xc000114ab0} 0x11d0ee0} 204
2022/03/01 20:42:40 Machine/s started successfully.
2022/03/01 20:42:40 &{0x1120700 {0xc000114ab0} 0x11d0ee0} 204
2022/03/01 20:42:40 Machine/s started successfully.
2022/03/01 20:42:40 &{0x1120700 {0xc000114ab0} 0x11d0ee0} 204
2022/03/01 20:42:40 Machine/s started successfully.
2022/03/01 20:42:40 &{0x1120700 {0xc000114ab0} 0x11d0ee0} 204
2022/03/01 20:42:40 Machine/s started successfully.
2022/03/01 20:42:40 &{0x1120700 {0xc000114ab0} 0x11d0ee0} 204
2022/03/01 20:42:40 Machine/s started successfully.

Shutting down one or more vm's

./vcenterapi -stop vm-51502 vm-5646 vm-69521
2022/03/01 01:34:45 [vm-51502 vm-5646 vm-69521]
2022/03/01 01:34:47 &{0x1120700 {0xc000100ab0} 0x11d0e00} 204
2022/03/01 01:34:47 &{0x1120700 {0xc000100ab0} 0x11d0e00} 204
2022/03/01 01:34:47 Machine/s stopped successfully.
2022/03/01 01:34:47 Machine/s stopped successfully.
2022/03/01 01:34:47 &{0x1120700 {0xc000100ab0} 0x11d0e00} 204
2022/03/01 01:34:47 Machine/s stopped successfully.

if machines are already in powered off the o/p is likely to be eg.:

2022/03/01 01:36:46 &{0x1120700 {0xc0001f6c60} 0x11d0e00} 400
2022/03/01 01:36:46 Problem stopping vm-5646, already in off state.

Terms and its meaning:

* ESXi is a product which provides virtualization.
* Vsphere is the name givne to the bundle of all features in the new version (ESXi, vcenter and its features so on).So ESXi is one of the product in vsphere.
github-actions[bot] commented 2 years ago

PR is not linked to any issue, please make the corresponding changes in the body. The issue should look like this. For help follow this link

ghost commented 2 years ago
👇 Click on the image for a new way to code review - Make big changes easier — review code in small groups of related files - Know where to start — see the whole change at a glance - Take a code tour — explore the change with an interactive tour - Make comments and review — all fully sync’ed with github [Try it now!](https://app.codesee.io/r/reviews?pr=1408&src=https%3A%2F%2Fgithub.com%2FHarshCasper%2FRotten-Scripts)

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map Legend