python update-from-esx_通过pyVmomi操作VCenter或ESXi获取所有vm

这是一个Python脚本,通过pyVmomi库连接到VCenter或ESXi,检索并打印出所有虚拟机(VM)的相关信息,包括IP地址、MAC地址、操作系统、CPU核心数、内存大小、电源状态等。
摘要由CSDN通过智能技术生成

#!/usr/bin/env python

# -*- coding: utf-8 -*-

# @Author : Eric Winn

# @Email : eng.eric.winn@gmail.com

# @Time : 2018/9/9 15:20

# @Version : 1.0

# @File : get_esxi_and_vm

# @Software : PyCharm

import argparse

import atexit

import ssl

from pyVim import connect

from pyVmomi import vmodl,vim

def help_parser():

"""

Builds a standard argument parser with arguments for talking to vCenter

-s service_host_name_or_ip

-o optional_port_number

-u required_user

-p optional_password

"""

parser = argparse.ArgumentParser(

description='Standard Arguments for talking to vCenter or ESXi')

parser.add_argument('-s', '--host',

required=True,

action='store',

help='vSphere service to connect to')

parser.add_argument('-o', '--port',

type=int,

default=443,

action='store',

help='Port to connect on')

parser.add_argument('-u', '--user',

required=True,

action='store',

help='User name to use when connecting to host')

parser.add_argument('-p', '--password',

required=True,

action='store',

help='Password to use when connecting to host')

return parser

def parse_service_instance(service_instance):

'''

:param service_instance:

:return:

'''

content = service_instance.RetrieveContent()

object_view = content.viewManager.CreateContainerView(content.rootFolder,

[], True)

for obj in object_view.view:

if isinstance(obj, vim.ComputeResource):

if isinstance(obj, vim.ClusterComputeResource):

print('VcenterCluster: {}'.format(obj.name))

for h in obj.host:

nic = h.config.network.vnic[0].spec

esxi_config = h.summary.config

print('''

ip:{}

mac:{}

os:{}

hostname:{}

'''.format(nic.ip.ipAddress, nic.mac, esxi_config.product.fullName, esxi_config.name))

for vx in h.vm:

if vx.summary.config.template is False:

for device in vx.config.hardware.device:

if (device.key >= 4000) and (device.key < 5000):

print('''

name:{}

ip:{}

cpu_cores:{}

memory:{}

mac:{}

hostname:{}

power_state:{}

'''.format(vx.name,

vx.summary.guest.ipAddress,

vx.summary.config.numCpu,

vx.summary.config.memorySizeMB,

device.macAddress,

vx.name,

str(vx.summary.runtime.powerState),

))

else:

print('VcenterESXi: {}'.format(obj.name))

for v in obj.host:

nic = v.config.network.vnic[0].spec

esxi_config = v.summary.config

print('''

ip:{}

mac:{}

os:{}

hostname:{}

'''.format(nic.ip.ipAddress, nic.mac, esxi_config.product.fullName, esxi_config.name))

for vx in v.vm:

if vx.summary.config.template is False:

for device in vx.config.hardware.device:

if (device.key >= 4000) and (device.key < 5000):

print('''

name:{}

ip:{}

cpu_cores:{}

memory:{}

mac:{}

hostname:{}

power_state:{}

'''.format(vx.name,

vx.summary.guest.ipAddress,

vx.summary.config.numCpu,

vx.summary.config.memorySizeMB,

device.macAddress,

vx.name,

str(vx.summary.runtime.powerState),

))

object_view.Destroy()

return 0

def makeConnect(parser):

"""

:return:

"""

try:

context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)

context.verify_mode = ssl.CERT_NONE

service_instance = connect.SmartConnect(

host=parser.host,

user=parser.user,

pwd=parser.password,

port=parser.port,

sslContext=context

)

if not service_instance:

print("Could not connect to the specified host using specified "

"username and password")

return -1

atexit.register(connect.Disconnect, service_instance)

# ## Do the actual parsing of data ## #

parse_service_instance(service_instance)

except vmodl.MethodFault as e:

return -1

return 0

if __name__ == '__main__':

parser = help_parser()

parser = parser.parse_args()

content = makeConnect(parser)

print(content)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值