需求:某客户短时间内需要申请大量的IP,并且在同一个C段(256个)
目的:通过SDK方式,将一个C段的IP都开出来
步骤:
一、配置环境:
安装CLI:
(1)安装python3.6
yum install python36
(2)安装pip
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py
(3)修改bashrc
[root@junper ~]# echo 'eval "$(register-python-argcomplete jdc)"' >> .bashrc
[root@junper ~]# echo 'export COLUMNS=100' >> .bashrc
[root@junper ~]# source ~/.bashrc
(4)配置Key
[root@junper ~]#
add --profile default --access-key **** --secret-key ***
(5)安装Python SDK
[root@junper ~]# pip install -U jdcloud_sdk
(5)脚本
import sys
from jdcloud_sdk.core.credential import Credential
from jdcloud_sdk.core.logger import Logger
from jdcloud_sdk.services.vpc.client.VpcClient import VpcClient
from jdcloud_sdk.services.vpc.apis.CreateElasticIpsRequest import CreateElasticIpsRequest # ak/sk配置
access_key = sys.argv[1]
secret_key = sys.argv[2] # 公网ip地域
# 华北-北京: cn-north-1
# 华东-上海: cn-east-2
# 华南-广州: cn-south-1
regionId = sys.argv[3] # 公网ip带宽1-200
bandwidthMbps = int(sys.argv[4]) # 获取client
def getVpcClient(accessKey, secretKey):
credential = Credential(accessKey, secretKey)
# 日志级别
log_level = 2
logger = Logger(log_level)
client = VpcClient(credential, logger=logger)
return client # 创建弹性公网ip
def createEip():
client = getVpcClient(access_key, secret_key)
try:
# 创建弹性公网ip参数
parameters = {
"maxCount": 1,
"elasticIpSpec": {
"provider": "bgp",
"bandwidthMbps": bandwidthMbps,
"chargeSpec": {
"chargeMode": "postpaid_by_usage"
}
},
"elasticIpAddress": "",
"regionId": regionId
}
with open("ips.txt", ) as f:
ipList = f.readlines() for ip in ipList:
ip = ip.strip()
parameters["elasticIpAddress"] = ip
request = CreateElasticIpsRequest(parameters)
resp = client.send(request)
print(ip, "requestId: ", resp.request_id)
if resp.error is not None:
print(resp.error.code, resp.error.message)
continue
print(resp.result)
except Exception as e:
print(e) if __name__ == "__main__":
createEip()
二、使用方法
1. 使用seq 1 254 | awk '{print "114.19.7."$1}' > ips.txt命令,将待创建的ip输出到ips.txt文件中,seq 1 254会生成1到254的序列,追加到114.19.7.的后面,构成完成的ip
2. 执行python3 createips.py AKSK 地域 公网带宽 > creatIP.log
公网ip地域:
华北-北京: cn-north-1
华东-上海: cn-east-2
华南-广州: cn-south-1
例如: python3 createips.py DADWFSFGSFEFSDGS8888ABHSSEEEEEE6 9D228E38283D8E8SFGSSDFSGSFEEEEEE cn-north-1 1 > createIP.log