| 2019-05-17
查看Python版本
python3 -V
安装腾讯云Python SDK
pip install -i https://mirrors.tencent.com/pypi/simple/ --upgrade tencentcloud-sdk-python
import json from time import time 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.lighthouse.v20200324 import lighthouse_client, models start = time() aria = ['ap-beijing', 'ap-chengdu', 'ap-guangzhou', 'ap-hongkong', 'ap-shanghai', 'ap-singapore', 'na-siliconvalley', 'eu-moscow', 'ap-tokyo', 'ap-nanjing', 'ap-mumbai', 'eu-frankfurt'] # 此处添加SecretId 与 SecretKey cred = credential.Credential("SecretId", "SecretKey") httpProfile = HttpProfile() httpProfile.endpoint = "lighthouse.tencentcloudapi.com" clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile for i in range(12): client = lighthouse_client.LighthouseClient(cred, aria[i], clientProfile) try: # 查看所有实例 req = models.DescribeInstancesRequest() params = { } req.from_json_string(json.dumps(params)) resp = client.DescribeInstances(req) response = json.loads(resp.to_json_string()) # print(response) # 实例详细信息 basic = response['InstanceSet'] # 判断地域是否含有实例 if response['TotalCount'] > 0: print(aria[i] + '实例数为' + str(response['TotalCount'])) # 提取返回的json信息 for ii in range(response['TotalCount']): ii1 = basic[ii] id = ii1['InstanceId'] # 修改实例密码 password = input('请输入你想更改的密码(请注意需要符合密码规则):') try: req2 = models.ResetInstancesPasswordRequest() params2 = { "InstanceIds": [id], "Password": password } req2.from_json_string(json.dumps(params2)) resp2 = client.ResetInstancesPassword(req2) response2 = json.loads(resp2.to_json_string()) # print(response2) print('更改成功!正在重启') except TencentCloudSDKException as err: print(err) try: req3 = models.RebootInstancesRequest() params3 = { "InstanceIds": [id] } req3.from_json_string(json.dumps(params3)) resp3 = client.RebootInstances(req3) response3 = json.loads(resp3.to_json_string()) print('重启命令发送成功!') except TencentCloudSDKException as err: print(err) except TencentCloudSDKException as err: print(err) end = time() print('本次代码执行共耗时:', round(end - start, 2), 's')
编辑:航网科技 来源:腾讯云 本文版权归原作者所有 转载请注明出处
微信扫一扫咨询客服
全国免费服务热线0755-36300002