Skip to content

Instantly share code, notes, and snippets.

View hartsock's full-sized avatar

Shawn Hartsock hartsock

View GitHub Profile
@hartsock
hartsock / ding.sh
Last active August 29, 2015 14:24 — forked from benpickles/ding.sh
# .___.__
# __| _/|__| ____ ____
# / __ | | |/ \ / ___\
# / /_/ | | | | \/ /_/ >
# \____ | |__|___| /\___ /
# \/ \//_____/
#
# Make a "ding" sound on your Mac! Useful for notifying you when a command
# line script has finished.
#
package com.neolitec.examples;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@hartsock
hartsock / test_vm_to_mac.py
Created April 13, 2015 15:09
This is a quick and dirty sample that shows how to go from a VM's name to a mac address by using the inventory path (more robust than a simple name)
def test_vm_to_mac(self):
si = connect.SmartConnect(host='vcsa',
user='my_user',
pwd='my_password')
content = si.RetrieveContent()
# where the name of the VM is 'box'
vm = content.searchIndex.FindByInventoryPath("Datacenter0/vm/box")
self.assertEqual(vm.name, 'box')
nics = [dev for dev in vm.config.hardware.device
if isinstance(dev, vim.vm.device.VirtualEthernetCard)]
@hartsock
hartsock / vswitch_vm_listing.py
Last active March 16, 2022 23:04
A quick and dirty script to list all the vms attached to a network ... by datacenter.
from __future__ import print_function
from pyVim import connect
def list_all_networks_and_vms():
si = connect.SmartConnect(host='vcsa',
user='root',
pwd='my_password')
root_folder = si.content.rootFolder
datacenters = [entity for entity in root_folder.childEntity
if hasattr(entity, 'networkFolder')]
@hartsock
hartsock / auto_power_on.py
Created December 29, 2014 18:45
A snippet from collaboration to develop a spec that you can feed to autoStartManager
testvm = # handwaving
spec = host.configManager.autoStartManager.config
auto_power_info = vim.host.AutoStartManager.AutoPowerInfo()
auto_power_info.key = testvm
auto_power_info.startAction = vim.VirtualMachine.PowerState.poweredOn #note use of constant instead of string
auto_power_info.startDelay = 60
auto_power_info.startOrder = 1
auto_power_info.stopAction = None # note: the Python constant 'None' not a string
auto_power_info.stopDelay = -1 # note the use of numeric value here.
@hartsock
hartsock / hosts_listing.py
Created December 22, 2014 22:08
A quick and dirty script to get all the hosts in a vCenter.
#!/usr/bin/env python
from __future__ import print_function
from pyVim import connect
from pyVmomi import vim
si = connect.SmartConnect(host='vcsa', user='my_user', pwd='my_password')
content = si.RetrieveContent()
@hartsock
hartsock / mount_a_disk.py
Last active August 29, 2015 14:10
A very rough sample that demonstrates mounting a disk on a VM.
si = connect.SmartConnect(host='vcsa',
user='my_user',
pwd='my_password')
content = si.RetrieveContent()
datacenters = [entity for entity in content.rootFolder.childEntity
if hasattr(entity, 'vmFolder')]
dc = None
for datacenter in datacenters:
@hartsock
hartsock / create_a_new_virtual_disk.py
Last active August 29, 2015 14:10
A snippet that creates a new virtual disk
si = connect.SmartConnect(host='vcsa', user='my_user', pwd='my_password')
content = si.RetrieveContent()
# the details we will need to make the disk:
disk_path = '[my_storage] my/path/to/my.vmdk'
adapter_type = vim.VirtualDiskManager.VirtualDiskAdapterType.lsiLogic
disk_type = vim.VirtualDiskManager.VirtualDiskType.preallocated
capacity_kb = 16 * 1024 * 1024 # some arbitrary size
# the spec for the disk:
@hartsock
hartsock / find_virtual_machine_by_datastore_path.py
Created December 4, 2014 00:23
Find a virtual machine by it's datastore path
si = connect.SmartConnect(host='vcsa', user='my_user', pwd='my_password')
content = si.RetrieveContent()
datacenters = [entity for entity in content.rootFolder.childEntity
if hasattr(entity, 'vmFolder')]
dc = None
for datacenter in datacenters:
if datacenter.name == 'my_datacenter':
dc = datacenter
@hartsock
hartsock / datacenters_listing.py
Created December 3, 2014 23:31
Get all the datacenters in a vCenter using pyVmomi
from __future__ import print_function
from pyVim import connect
from pyVmomi import vim
si = connect.SmartConnect(host='vcsa', user='my_user', pwd='my_password')
content = si.RetrieveContent()
# A list comprehension of all the root folder's first tier children...
datacenters = [entity for entity in content.rootFolder.childEntity