Let's say there are a multitude of ESXi servers in your environment and you'd like to manage some virtual machines through cli.
One way to do this is to run vim-cmd commands over ssh. For example, I have 2 ESXis named in the example esxi1 and esxi2. On both of them I have one VM:
~$ ssh root@esxi1.localdomain "vim-cmd vmsvc/getallvms"
Vmid Name File Guest OS Version Annotation
7 Linux [datastore1] Linux/Linux.vmx debian6_64Guest vmx-08
~$ ssh root@esxi2.localdomain "vim-cmd vmsvc/getallvms"
Password:
Vmid Name File Guest OS Version Annotation
13 VSRX [datastore1] VSRX/VSRX.vmx otherGuest vmx-09 VSRX OVF Template
So, this works fine and nice and actually there's nothing special about it. It's just running remote commands over ssh.If you'd like to make things more easy, you can use ssh public key authentication for the remote esxi hosts (no need to type in the password every time you want to run a command).
I wrote some bash functions to make it even easier (to remember) and shorter to type.
This is what I have among other functions and things in my .bashrc file:
function start_vm () { ssh root@${1}.localdomain "vim-cmd vmsvc/power.on" "$2";}
function stop_vm () { ssh root@${1}.localdomain "vim-cmd vmsvc/power.off" "$2";}
function reboot_vm () { ssh root@${1}.localdomain "vim-cmd vmsvc/power.reboot" "$2";}
function getallvm () { ssh root@${1}.localdomain "vim-cmd vmsvc/getallvms" ;}
function powerstate_vm () { ssh root@${1}.localdomain "vim-cmd vmsvc/power.getstate" "$2";}
function getnetwrorks () { ssh root@${1}.localdomain "vim-cmd vmsvc/get.networks" "$2";}
Where $1 and $2 are the first (esxi host) and second (where applicable in the above functions it is the VM ID) arguments after the function call.
To give an example of using the functions:
~$ getallvm esxi1
Vmid Name File Guest OS Version Annotation
7 Linux [datastore1] Linux/Linux.vmx debian6_64Guest vmx-08
~$ powerstate_vm esxi1 7
Retrieved runtime info
Powered on
~$ stop_vm esxi1 7
Powering off VM:
~$ powerstate_vm esxi1 7
Retrieved runtime info
Powered off
~$ getallvm esxi2
Vmid Name File Guest OS Version Annotation
13 VSRX [datastore1] VSRX/VSRX.vmx otherGuest vmx-09 VSRX OVF Template
~$ getnetwrorks esxi2 13
Networks:
(vim.Network.Summary) {
dynamicType = ,
network = 'vim.Network:HaNetwork-VM Network',
name = "VM Network",
accessible = true,
ipPoolName = "",
ipPoolId = ,
}
(vim.Network.Summary) {
dynamicType = ,
network = 'vim.Network:HaNetwork-vmnic3-novlan',
name = "vmnic3-novlan",
accessible = true,
ipPoolName = "",
ipPoolId = ,
}
(vim.Network.Summary) {
dynamicType = ,
network = 'vim.Network:HaNetwork-vmnic2-novlan',
name = "vmnic2-novlan",
accessible = true,
ipPoolName = "",
ipPoolId = ,
}
No comments:
Post a Comment