利用Python调用云Api实现全地域同步轻量应用服务器密钥并共享给云服务器

2022-04-06 12:32:13 浏览数 (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
import tencentcloud.cvm.v20170312.cvm_client
import tencentcloud.cvm.v20170312.models
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()
lh_aria = ['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
key_name = input('key_name=')

client = lighthouse_client.LighthouseClient(cred, 'ap-beijing', clientProfile)

try:
    req = models.CreateKeyPairRequest()
    params = {
        "KeyName": key_name
    }
    req.from_json_string(json.dumps(params))

    resp = client.CreateKeyPair(req)

    response = json.loads(resp.to_json_string())
    # print(response)
    public_key = response['KeyPair']['PublicKey']
    private_key = response['KeyPair']['PrivateKey']
    # key_aria = key_name '_' aria[i]

    key = open(key_name, mode='w')
    key.write(private_key)
    key.close()
    print('私钥已输出至文件:', key_name)
    # print(resp.to_json_string())
    for i in range(11):
        try:
            client1 = lighthouse_client.LighthouseClient(cred, lh_aria[i], clientProfile)
            req1 = models.ImportKeyPairRequest()
            params1 = {
                "KeyName": key_name,
                "PublicKey": public_key
            }
            req1.from_json_string(json.dumps(params1))

            resp1 = client1.ImportKeyPair(req1)
            print(resp1.to_json_string())

        except TencentCloudSDKException as err:
            print(err)

    try:

        httpProfile2 = HttpProfile()
        httpProfile2.endpoint = "cvm.tencentcloudapi.com"

        clientProfile2 = ClientProfile()
        clientProfile2.httpProfile2 = httpProfile2
        client2 = tencentcloud.cvm.v20170312.cvm_client.CvmClient(cred, 'ap-beijing', clientProfile2)

        req2 = tencentcloud.cvm.v20170312.models.ImportKeyPairRequest()
        params2 = {
            "KeyName": key_name,
            "ProjectId": 0,
            "PublicKey": public_key
        }
        req2.from_json_string(json.dumps(params2))

        resp2 = client2.ImportKeyPair(req2)
        print(resp2.to_json_string())
    except TencentCloudSDKException as err:
        print(err)

except TencentCloudSDKException as err:
    print(err)
end = time()
print('本次代码执行共耗时:', round(end - start, 2), 's')

0 人点赞