前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >利用Python调用云Api实现多地域同步创建TAT命令

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

原创
作者头像
小宇-xiaoyu
发布2022-03-23 14:04:05
2.4K2
发布2022-03-23 14:04:05
举报
文章被收录于专栏:玩转Lighthouse.玩转Lighthouse.

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 base64,random,string
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.tat.v20201028 import tat_client, models

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 = "tat.tencentcloudapi.com"
# command
ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 8))
input('按回车开始输入命令,输入完毕输入:wq 按回车结束')
stopword = ':wq' # 输入停止符
string = ''
for line in iter(input, stopword):
  string += line + '\n'
encodestr = base64.b64encode(string.encode('utf-8'))
Content = str(encodestr, 'utf-8')
print(Content)

CommandName = input('命令名称:')or ran_str
Description = input('命令描述:')
CommandType = input('命令类型(SHELL、POWERSHELL):') or "SHELL"
Timeout = input('超时时间(默认60 取值范围[1, 86400]:)') or 60

clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
for i in range(12):
    client = tat_client.TatClient(cred, aria[i], clientProfile)
    try:
        req = models.CreateCommandRequest()
        params = {
            "CommandName": "{0}".format(CommandName),
            "Description": "{0}".format(Description),
            "Content": "{0}".format(Content),
            "CommandType": "{0}".format(CommandType),
            "Timeout": Timeout
        }
        req.from_json_string(json.dumps(params))

        resp = client.CreateCommand(req)
        print(resp.to_json_string())

    except TencentCloudSDKException as err:
        print(err)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 0.准备工作
  • 1.SDK下载
  • 2.代码部分
相关产品与服务
自动化助手
自动化助手(TencentCloud Automation Tools,TAT)是云服务器的原生运维部署工具。TAT提供自动化的远程操作方式,可直接管理实例,批量执行 Shell,Powershell,Python等命令,轻松完成运行自动化运维脚本、轮询进程、安装或卸载软件、更新应用以及安装补丁等常见管理任务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档