开发者社区> 问答> 正文

Python 2.7.5中的for循环

我是Python的新手,正在尝试编写一个脚本来为在多个位置运行的所有VMWare vCenter创建清单报告。该脚本基本上使用我的凭据在vCenter中创建会话ID,然后运行REST API调用以从vCenter获取所需信息。我可以为一个vCenter运行它,但是我想进行修改,使其应该一个一个地读取所有vCenter列表,并为每个vCenter执行所有REST API请求并生成输出,如下所示。你能帮忙吗?

输入文件格式:

vcenter1.abc.com
vcenter2.abc.com
vcenter3.xyz.com
vcenter4.xyz.com

我当前的一个vCenter脚本:

#!/usr/bin/python
import requests
import json

vcenter="vcenter1.abc.com"

sess = requests.post(
    "https://vcenter1.abc.com/rest/com/vmware/cis/session",
    auth=('vcusr_testapi@vsphere.local', 'xxxxxxx'),
    verify=False
)

session_id = sess.json()['value']

print 'vcenter,#datacenters,#clusters,#Hosts,#HostsConnected,#HostsDisconnected,#VMs,#PoweredOnVMs,#PoweredOffVMs'

resp = requests.get(
    "https://vcenter1.abc.com/rest/vcenter/datacenter",
    verify=False,
    headers={
        "vmware-api-session-id": session_id
     }
)

vc_response=json.loads(resp.text)
list_dc=vc_response["value"]
count_dc=len(list_dc)

resp = requests.get(
    "https://vcenter1.abc.com/rest/vcenter/cluster",
    verify=False,
    headers={
        "vmware-api-session-id": session_id
    }
)

vc_response=json.loads(resp.text)
list_cluster=vc_response["value"]
count_cluster=len(list_cluster)

resp = requests.get(
    "https://vcenter1.abc.com/rest/vcenter/host",
    verify=False,
    headers={
        "vmware-api-session-id": session_id
    }
)

host_response=json.loads(resp.text)
list_host=host_response["value"]
count_host=len(list_host)

resp = requests.get(
    "https://vcenter1.abc.com/rest/vcenter/host?filter.connection_states=CONNECTED",
    verify=False,
    headers={
        "vmware-api-session-id": session_id
    }
)

host_response=json.loads(resp.text)
list_host_connect=host_response["value"]
count_host_connect=len(list_host_connect)

resp = requests.get(
    "https://vcenter1.abc.com/rest/vcenter/host?filter.connection_states=DISCONNECTED",
    verify=False,
    headers={
        "vmware-api-session-id": session_id
    }
)

host_response=json.loads(resp.text)
list_host_disconnect=host_response["value"]
count_host_disconnect=len(list_host_disconnect)

resp = requests.get(
    "https://vcenter1.abc.com/rest/vcenter/vm",
    verify=False,
    headers={
        "vmware-api-session-id": session_id
    }
)

vc_response=json.loads(resp.text)
list_vm=vc_response["value"]
count_vm=len(list_vm)

resp = requests.get(
    "https://vcenter1.abc.com/rest/vcenter/vm?filter.power_states=POWERED_OFF",
    verify=False,
    headers={
        "vmware-api-session-id": session_id
    }
)

vc_response=json.loads(resp.text)
list_vm_on=vc_response["value"]
count_vm_on=len(list_vm_on)

resp = requests.get(
    "https://vcenter1.abc.com/rest/vcenter/vm?filter.power_states=POWERED_ON",
    verify=False,
    headers={
        "vmware-api-session-id": session_id
    }
)

vc_response=json.loads(resp.text)
list_vm_off=vc_response["value"]
count_vm_off=len(list_vm_off)

print vcenter,count_dc,count_cluster,count_host,count_host_connect,count_host_disconnect,count_vm,count_vm_on,count_vm_off

##print(vcenter, count_dc, sep=",")

当前脚本的输出:

vcenter,#datacenters,#clusters,#Hosts,#HostsConnected,#HostsDisconnected,#VMs,#PoweredOnVMs,#PoweredOffVMs
vcenter1.abc.com 5 6 16 14 2 389 140 249

我想以逗号分隔格式打印变量输出的输出,类似于输出文件中的标题行,请使用语法帮助我。

另外,需要修改脚本以使用for循环或任何其他机制为所有vCenter运行脚本。

我正在使用Python 2.7.5。

展开
收起
几许相思几点泪 2019-12-29 19:43:45 1051 0
1 条回答
写回答
取消 提交回答
  • 这是脚本的重写版本,可处理vCenter列表并对其执行所有请求(不过请耐心等待,因为我没有环境可以这样做):

    !/usr/bin/python
    from __future__ import print_function
    import requests
    import json
    
    vcenters = (
        'vcenter1.abc.com',
        'vcenter2.abc.com',
        'vcenter3.xyz.com',
        'vcenter4.xyz.com'
    )
    
    apis = dict(
        dcs = '/rest/vcenter/datacenter',
        clusters = '/rest/vcenter/cluster',
        hosts = '/rest/vcenter/host',
        connected = '/rest/vcenter/host?filter.connection_states=CONNECTED',
        disconnected = '/rest/vcenter/host?filter.connection_states=DISCONNECTED',
        vms = '/rest/vcenter/vm',
        powered_off = '/rest/vcenter/vm?filter.power_states=POWERED_OFF',
        powered_on = '/rest/vcenter/vm?filter.power_states=POWERED_ON',
    )
    
    auth = ('vcusr_testapi@vsphere.local', 'xxxxxxx')
    
    counter = {}
    
    for vcenter in vcenters:
        counter[vcenter] = {}
        session = requests.post(
            'https://%s/rest/com/vmware/cis/session' % vcenter,
            auth=auth,
            verify=False
        )
    
        kwargs = dict(
            headers = dict(
                vmware-api-session-id = session.json()['value']
            ),
            verify=False
        )
    
        for api, url in apis.items():
            r = requests.get(
                'https://%s%s' % (vcenter, url),
                *kwargs
            )
    
            counter[vcenter][api] = json.loads(r.text)['value']
    
    print('vcenter,#datacenters,#clusters,#Hosts,#HostsConnected,#HostsDisconnected,#VMs,#PoweredOnVMs,#PoweredOffVMs')
    for vcenter in counter:
        count = counter[vcenter]
        print(
            vcenter,
            count['dcs'],
            count['clusters'],
            count['hosts'],
            count['connected'],
            count['disconnected'],
            count['vms'],
            count['powered_on'],
            count['powered_off',
            sep=','
        )
    
    

    我希望它至少可以作为一个起点。

    2019-12-29 19:44:08
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载