由於自己管理的雲服務器數量比較多,時不時需要更換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)
創建了一個微信群,目前看來是純技術,雖然大家技術都不咋樣,歡迎菜鳥或大牛的加入,目前看來還能算是個純技術的討論群,勿培訓,勿賣視頻

