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)