SoftLayer API 了解 VLAN 中的总 IP 和可用 IP

SoftLayer API to know and total and available IPs in a VLAN

SoftLayer API 了解 VLAN 中的总 IP 和可用 IP

您好,

如果我知道 VLAN ID.

可以使用哪个 API 来知道总 IP 和 VLAN 的 used/usable IP

我能想到的一种方法是我可以获得 VLAN 的子网,然后在子网详细信息中我可以看到具有 "totalIpAddresses,usableIpAddressCount" 属性的总 IP 和可用 IP。但是,由于 VLAN 具有多个子网,因此我将必须获得 VLAN 的总 IP 和可用 IP 的总和。不确定这是否正确。

谢谢

请尝试以下操作:

 https://[username]:[apikey]@api.softlayer.com/rest/v3/SoftLayer_Network_Vlan/[Vlan_id]/getObject?objectMask=mask[subnets[ipAddresses]]
Method: GET

或者,

如果您希望此请求通过 IP 的关联子网检索与 IP 地址关联的 VLAN

URL:

https://[username]:[apikey]@api.softlayer.com/rest/v3/SoftLayer_Network_Vlan/getVlanForIpAddress?objectMask=mask[id, vlanNumber,networkSpace,primaryRouter]
Method: POST

Json:

{
  "parameters": [
    "10.41.160.194"
  ]
}

参考: getVlanForIpAddress

你还可以看到上面请求的'Subnet'部分显示的数据的含义,即:

totalIpAddresses:此子网中包含的 IP 地址数。

usableIpAddressCount: 可在该子网内寻址的 IP 地址数。

要获取有关其子网的 vlan 总 used/usable 个 IP 的信息,请尝试以下 Python 脚本。

此脚本将有助于从 vlan 内的子网中获取空闲插槽的确切数量。

"""
This script retrieves the Total Ip Addresses, Usable Ip Address Count and Free Slots for an specific Vlan Id

Important manual pages:
http://sldn.softlayer.com/reference/services/SoftLayer_Account/getNetworkVlans
http://sldn.softlayer.com/article/object-masks
http://sldn.softlayer.com/article/object-filters

License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
"""
import SoftLayer
from prettytable import PrettyTable

# Declare your SoftLayer username and apiKey
username = 'set me'
apikey = 'set me'

# Define the vlan Id
vlanId = 318734

# Contact To SoftLayer
client = SoftLayer.Client(username=username, api_key=apikey)

# Declare an Object Mask to get additional information
object_mask = 'mask[primaryRouter,primarySubnet[datacenter[name]],subnets[billingItem, subnetType,networkIdentifier, cidr, totalIpAddresses, usableIpAddressCount, ipAddresses[ipAddress, isReserved, virtualGuest, hardware]]]'
# Declare an Object Filter to get information from specific vlan
filter = {'networkVlans': {'id': {'operation': vlanId}}}


result = client['SoftLayer_Account'].getNetworkVlans(mask=object_mask, filter=filter)
x = PrettyTable(["Vlan Id", "Vlan Number", "Subnet", "Total Ip Addresses", "Usable Ip Address Count","Free Slots"])

count = 0
for vlan in result:
    for subnet in vlan['subnets']:
        for item in subnet['ipAddresses']:
            if item['isReserved'] == True:
                count = count + 1
            if 'hardware' in item:
                count = count + 1
            if 'virtualGuest' in item:
                count = count + 1
        if (subnet['usableIpAddressCount'] - count) > 0:
            if subnet['subnetType'] == 'PRIMARY' or subnet['subnetType'] == 'ADDITIONAL_PRIMARY':
                x.add_row([vlan['id'], str('%s  %s' % (vlan['primaryRouter']['hostname'], vlan['vlanNumber'])), str('%s/%s' % (subnet['networkIdentifier'], subnet['cidr'])), subnet['totalIpAddresses'], subnet['usableIpAddressCount'], (subnet['usableIpAddressCount'] - count)])
        count = 0
print(x)

参考资料: SoftLayer_Account::getNetworkVlans