1. 程式人生 > >騰訊雲API彈性公網IP踩坑

騰訊雲API彈性公網IP踩坑

由於自己管理的雲伺服器數量比較多,時不時需要更換IP,在管理臺上一下下點選,實在浪費時間,於是就想到了通過API呼叫的方式,將更換IP一系列動作,全部整合到Python程式碼裡面,實現一行命令,完成IP更換,由於人懶,就先

把最核心的程式碼(雖然都是騰訊雲生成的)、流程、坑點貼出來,僅供菜鳥參考,高手無視!已先在騰訊雲社群編輯釋出,請管理員勿認為是轉載!

具體步驟:

一 進入 https://cloud.tencent.com/document/api ,

頁面左側列表查詢“私有網路”---“彈性公網相關介面”,就可以看到對應介面的文件了

二 選擇一個介面,然後點選“API 3.0 Exploper”,進入到開發者工具頁面,

坑點一:下面程式碼,對於菜鳥來說,引入的相關模組直接用“pip install 對應模組名”會報錯

看第一張圖sdk,相關語言sdk,點選就可以看到github上各語言的庫引用方式

三 直接copy開發者工具上的程式碼,當然你也可以看下圖程式碼

坑點二:"SecretId ","SecretKey" 這兩個值是你呼叫API,騰訊用來確認你身份的憑證,

在哪裡申請呢?騰訊雲的首頁,“雲產品”--“管理工具”--“雲API祕鑰”,

沒有使用過的話,是不會在你的控制檯上顯示的。

坑點三:由於騰訊雲API文件的不合理,導致生成的程式碼有一些坑,

具體坑資訊我已在程式碼裡通過註釋的方式解釋了

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.vpc.v20170312 import vpc_client, models
 
#查詢彈性IP
def findIp():
    try:
        cred = credential.Credential("SecretId ", "SecretKey")
        httpProfile = HttpProfile()
        httpProfile.endpoint = "vpc.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = vpc_client.VpcClient(cred, "ap-chongqing", clientProfile)
        req = models.DescribeAddressesRequest()
        params = '{}'
        req.from_json_string(params)
        resp = client.DescribeAddresses(req)
        #eip=resp.to_json_string()[34:61]
        eip=resp.to_json_string()[48:60]
        print(eip)   #列印結果:"AddressId": "eip-ilzg91oy"
        return eip
    except TencentCloudSDKException as err:
        print(err)
 
#解除繫結IP
def unbindingIp():
    try:
        cred = credential.Credential("SecretId ", "SecretKey")
        httpProfile = HttpProfile()
        httpProfile.endpoint = "vpc.ap-chongqing.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = vpc_client.VpcClient(cred, "ap-chongqing", clientProfile)
        req = models.DisassociateAddressRequest()
        eip = findIp()
        params = eip
        #req.from_json_string(params)
        req.AddressId=params   #這裡修改了一下,官網生成的是上一行程式碼
        resp = client.DisassociateAddress(req)
        print(resp.to_json_string())
    except TencentCloudSDKException as err:
        print(err)
#釋放IP
def releaseIp():
    try:
        cred = credential.Credential("SecretId ", "SecretKey")
        httpProfile = HttpProfile()
        httpProfile.endpoint = "vpc.ap-chongqing.tencentcloudapi.com"
 
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = vpc_client.VpcClient(cred, "ap-chongqing", clientProfile)
 
        req = models.DescribeAddressesRequest()
        eip = findIp()
        list=[]
        list.append(eip)
        params = list
        req.AddressIds=params  #這裡修改了一下,要求傳陣列
        resp = client.ReleaseAddresses(req)
        print(resp.to_json_string())
    except TencentCloudSDKException as err:
        print(err)
 
#建立IP
def newIp():
    try:
        cred = credential.Credential("SecretId", "SecretKey")
        httpProfile = HttpProfile()
        httpProfile.endpoint = "vpc.ap-chongqing.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = vpc_client.VpcClient(cred, "ap-chongqing", clientProfile)
        req = models.AllocateAddressesRequest()
        params = '{}'
        req.from_json_string(params)
        resp = client.AllocateAddresses(req)
        print(resp.to_json_string())
 
    except TencentCloudSDKException as err:
        print(err)
 
#繫結彈性IP
def binding():
    try:
        cred = credential.Credential("SecretId", "SecretKey")
        httpProfile = HttpProfile()
        httpProfile.endpoint = "vpc.ap-chongqing.tencentcloudapi.com"
 
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = vpc_client.VpcClient(cred, "ap-chongqing", clientProfile)
 
        req = models.AssociateAddressRequest()
        #params = '{"ins-61lwor90"}'
        #req.from_json_string(params)
        #這是隻傳AddressId的報錯:MissingAssociateEntity message:You need to specify the entity ID `"InstanceId"` or `"NetworkInterfaceId"` to associate this address. requestId:81702256-e75f-458f-afde-e87a69554f83
        #所以至少要傳兩個值
        req.InstanceId = "ins-61cwor70"
        eip = findIp()
        req.AddressId=eip
        resp = client.AssociateAddress(req)
        print(resp.to_json_string())
 
    except TencentCloudSDKException as err:
        print(err)

建立了一個微信群,目前看來是純技術,雖然大家技術都不咋樣,歡迎菜鳥或大牛的加入,目前看來還能算是個純技術的討論群,勿培訓,勿賣視訊