美文网首页
Python 获取腾讯云当前VPC的所有cvm实例列表

Python 获取腾讯云当前VPC的所有cvm实例列表

作者: 偷油考拉 | 来源:发表于2023-05-28 14:27 被阅读0次
    #!/usr/bin/python
    # coding=utf8
    import json
    from tencentcloud.common import credential
    from tencentcloud.common.profile.client_profile import ClientProfile
    from tencentcloud.common.profile.http_profile import HttpProfile
    from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
    from tencentcloud.cvm.v20170312 import cvm_client, models
    
    SecretId="xxxxxxxxxxxxxxxxxxxxxxxx"
    SecretKey="xxxxxxxxxxxxxxxxxxxxxxxx"
    
    try:
        cred = credential.Credential(SecretId, SecretKey)
        httpProfile = HttpProfile()
        httpProfile.endpoint = "cvm.tencentcloudapi.com"
    
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = cvm_client.CvmClient(cred, "ap-shanghai", clientProfile)
    
        req = models.DescribeInstancesRequest()
        #  根据 limit 和 offset 循环获取
        count = 1
        offsetx = 0
        limitx = 20
        params = {
            "Filters": [
                {
                    "Name": "vpc-id",
                    "Values": [ "vpc-xxxxxxxxx" ]
                }
            ],
            "Offset": 0,
            "Limit": limitx
        }
    
        while offsetx < count:
            if offsetx == 0:
                    params['Offset']=offsetx
                    req.from_json_string(json.dumps(params))
                    resp = client.DescribeInstances(req)
                    resptmp = resp.InstanceSet
                    count=resp.TotalCount
                    offsetx=offsetx+limitx
            else:
                    params['Offset']=offsetx
                    req.from_json_string(json.dumps(params))
                    resp = client.DescribeInstances(req)
                    resp.InstanceSet.extend(resptmp)
                    resptmp = resp.InstanceSet
                    count=resp.TotalCount
                    offsetx=offsetx+limitx
    
        #print(vars(resp))
        #print(resp.InstanceSet)
        print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
        for x in resp.InstanceSet:
          print("**************************************************************")
          #print(json.dumps(x,indent=2))
          print(x.OsName.split()[0])
          #print(x['Uuid'])
          #print(x['Tags'])
          #print(x['InstanceId'])
          #print(x['InstanceName'])
          #print(x['OsName'])
          #print(x['PrivateIpAddresses'])
          #print(x['PrivateIpAddresses'][0])
    except TencentCloudSDKException as err:
        print(err)
    

    相关文章

      网友评论

          本文标题:Python 获取腾讯云当前VPC的所有cvm实例列表

          本文链接:https://www.haomeiwen.com/subject/zbldedtx.html