利用Python调用云Api实现多地域同步运行TAT命令

2022-08-20 16:41:41 浏览数 (1)

0.准备工作

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

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

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

1.SDK下载

请确保Python版本为3.6

查看Python版本

代码语言:javascript复制
python3 -V

安装腾讯云Python SDK

代码语言:javascript复制
pip install -i https://mirrors.tencent.com/pypi/simple/ --upgrade tencentcloud-sdk-python

2.代码部分

代码语言:javascript复制
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.tat.v20201028 import tat_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', 'na-toronto']
# 此处添加SecretId 与 SecretKey
cred = credential.Credential("SecretId", "SecretKey")
httpProfile = HttpProfile()
httpProfile.endpoint = "tat.tencentcloudapi.com"

clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
httpProfile = HttpProfile()

cmd_name = input('命令名称:')
for i in range(len(aria)):
    client = tat_client.TatClient(cred, aria[i], clientProfile)
    try:
        req0 = models.DescribeCommandsRequest()
        params0 = {
            "Filters": [
                {
                    "Name": "command-name",
                    "Values": [cmd_name]
                }
            ]
        }
        req0.from_json_string(json.dumps(params0))

        resp0 = client.DescribeCommands(req0)
        response0 = json.loads(resp0.to_json_string())
        # print(response0)
        if response0['TotalCount'] == 1:
            cmd_id = response0['CommandSet'][0]['CommandId']
            print('已获取到' aria[i] '的命令id:' cmd_id)
            try:
                req = models.DescribeAutomationAgentStatusRequest()
                params = {
                }
                req.from_json_string(json.dumps(params))

                resp = client.DescribeAutomationAgentStatus(req)
                response = json.loads(resp.to_json_string())

                if len(response['AutomationAgentSet']) == 0:
                    continue
                else:
                    for q in range(len(response['AutomationAgentSet'])):
                        lh_id = response['AutomationAgentSet'][q]['InstanceId']
                        try:
                            req1 = models.InvokeCommandRequest()
                            params1 = {
                                "CommandId": cmd_id,
                                "InstanceIds": [lh_id]
                            }
                            req1.from_json_string(json.dumps(params1))

                            resp1 = client.InvokeCommand(req1)
                            response1 = json.loads(resp1.to_json_string())
                            # print(response1)
                            print('成功对服务器:' lh_id '执行了“%s”命令' % cmd_name)
                        except TencentCloudSDKException as err:
                            print(err)
            except TencentCloudSDKException as err:
                print(err)
        else:
            continue
    except TencentCloudSDKException as err:
        print(err)
end = time()
print('本次代码执行共耗时:', round(end - start, 2), 's')

效果如图:

0 人点赞