前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基础网络CVM使用API批量切换私有网络/批量回退基础网络

基础网络CVM使用API批量切换私有网络/批量回退基础网络

原创
作者头像
vic@TX
修改2023-05-09 19:03:37
2K0
修改2023-05-09 19:03:37
举报

一、迁移方案

基础网络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

三、操作步骤

1、准备待迁移实例列表

从控制台筛选导出实例信息

按照如下格式整理成csv表格(填写实例id、内网ip、目标vpc和子网)

2、准备迁移脚本

修改API密钥

修改目标实例所在地域,参考:云服务器 地域和可用区-产品简介-文档中心-腾讯云

从csv表格中读取实例id、内网ip、目标vpc、目标子网等参数:

调切换接口从基础网络切换到私有网络:

调回退接口从私有网络回退基础网络:

3、执行情况

批量切换私有网络

批量回退基础网络

四、脚本示例(python,供参考)

1、CVM批量切换私有网络

代码语言:javascript
复制
# -*- 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 )

2、CVM批量回退基础网络

代码语言:javascript
复制
# -*- 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 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、迁移方案
  • 二、环境准备
  • 三、操作步骤
    • 1、准备待迁移实例列表
      • 从控制台筛选导出实例信息
      • 按照如下格式整理成csv表格(填写实例id、内网ip、目标vpc和子网)
    • 2、准备迁移脚本
      • 修改API密钥
      • 修改目标实例所在地域,参考:云服务器 地域和可用区-产品简介-文档中心-腾讯云
      • 从csv表格中读取实例id、内网ip、目标vpc、目标子网等参数:
      • 调切换接口从基础网络切换到私有网络:
      • 调回退接口从私有网络回退基础网络:
    • 3、执行情况
      • 批量切换私有网络
      • 批量回退基础网络
  • 四、脚本示例(python,供参考)
    • 1、CVM批量切换私有网络
      • 2、CVM批量回退基础网络
      相关产品与服务
      私有网络
      私有网络(Virtual Private Cloud,VPC)是基于腾讯云构建的专属云上网络空间,为您在腾讯云上的资源提供网络服务,不同私有网络间完全逻辑隔离。作为您在云上的专属网络空间,您可以通过软件定义网络的方式管理您的私有网络 VPC,实现 IP 地址、子网、路由表、网络 ACL 、流日志等功能的配置管理。私有网络还支持多种方式连接 Internet,如弹性 IP 、NAT 网关等。同时,您也可以通过 VPN 连接或专线接入连通腾讯云与您本地的数据中心,灵活构建混合云。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档