基础网络CVM切换到私有网络,有两种方式:
1、在控制台切换CVM网络(单次最多批量切换20台),参考文档:
2、通过API方式切换网络,参考文档:
云服务器 修改实例vpc属性-API 文档-文档中心-腾讯云
本文以python为例,介绍调腾讯云SDK批量切换基础网络CVM的方法。
1、安装python(2.7或3.0以上版本都可以);
2、安装腾讯云最新SDK(需最新SDK才支持回退接口):
pip install tencentcloud-sdk-python
或pip3 install tencentcloud-sdk-python
# -*- coding: utf-8 -*-
# Copyright 2017-2021 Tencent Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import json
from re import A
import sys
import csv
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.cvm.v20170312 import cvm_client, models
try:
# 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
# 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
cred = credential.Credential("xxxx", "xxx")
# 实例化一个http选项,可选的,没有特殊需求可以跳过
httpProfile = HttpProfile()
httpProfile.endpoint = "cvm.tencentcloudapi.com"
# 实例化一个client选项,可选的,没有特殊需求可以跳过
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
# 实例化要请求产品的client对象,clientProfile是可选的,地域修改为实例所在地域,参考:https://cloud.tencent.com/document/product/213/6091
client = cvm_client.CvmClient(cred, "ap-xxxx", clientProfile)
except TencentCloudSDKException as err:
print(err)
if __name__ == '__main__':
#读取csv文件中的实例id、内网ip、目标VPC、目标子网等信息
with open("/xxx/cvm_list.csv",encoding="utf-8",mode="r") as f:
reader = csv.DictReader(f)
for row in reader:
print(row["ID"],row["主IPv4内网IP"],row["目标VPC"],row["目标子网"])
# 实例化一个请求对象,每个接口都会对应一个request对象
req = models.ModifyInstancesVpcAttributeRequest()
params = {
"InstanceIds": [row["ID"]],
"VirtualPrivateCloud": {
"VpcId": row["目标VPC"],
"SubnetId": row["目标子网"],
"PrivateIpAddresses": [row["主IPv4内网IP"]]
}
}
req.from_json_string(json.dumps(params))
# 返回的resp是一个ModifyInstancesVpcAttributeResponse的实例,与请求对象对应
resp = client.ModifyInstancesVpcAttribute(req)
# 输出json格式的字符串回包
print(resp.to_json_string())
# 加1秒等待时间,每秒切换1台
time.sleep( 1 )
# -*- coding: utf-8 -*-
# Copyright 2017-2021 Tencent Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import json
from re import A
import sys
import csv
import time
from tencentcloud.common.common_client import CommonClient
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
try:
cred = credential.Credential(
"AKIxxxx",
"dChxxxx")
httpProfile = HttpProfile()
# 域名首段必须和下文中CommonClient初始化的产品名严格匹配
httpProfile.endpoint = "cvm.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
# 实例化要请求的common client对象,clientProfile是可选的。
common_client = CommonClient("cvm", '2017-03-12', cred, "ap-guangzhou", profile=clientProfile)
except TencentCloudSDKException as err:
print(err)
if __name__ == '__main__':
#读取csv文件中的实例id、内网ip等信息
with open("/xxxx/cvm_list.csv",encoding="utf-8",mode="r") as f:
reader = csv.DictReader(f)
for row in reader:
print(row["ID"],row["主IPv4内网IP"])
#调用回退接口
print(common_client.call_json(
"ModifyInstancesVpcAttribute",
{"AllowBasicNetwork":True,"VirtualPrivateCloud":{},"InstanceIds":[row["ID"]],"BasicNetworkIps":[row["主IPv4内网IP"]]}))
# 加1秒等待时间,每秒切换1台
time.sleep( 1 )
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。