Python 获取腾讯云当前VPC的所有cvm实例列表
2023-05-28 本文已影响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)