随笔,利用Python调用云Api实现多地域快速修改轻量应用服务器密码

上一次我们学到了如何批量创建与绑定密钥 这次我们来进行批量修改密码
上集回顾:利用Python调用云Api实现多地域同步创建轻量应用服务器密钥并保存到本地

利用Python调用云Api实现批量绑定轻量应用服务器密钥

0.准备工作

使用本代码请先进行子用户创建并授权云API轻量应用服务器全部权限

请注意 为了保障您的账户以及云上资产的安全 请谨慎保管SecretId 与 SecretKey 并定期更新 删除无用权限

前往创建子用户:https://console.cloud.tencent.com/cam

1.SDK下载

请确保Python版本为3.6+

查看Python版本

python3 -V

安装腾讯云Python SDK

pip install -i https://mirrors.tencent.com/pypi/simple/ --upgrade tencentcloud-sdk-python

2.代码部分

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')

正文完