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

2023-12-31

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

Hello,

如果我知道某个 VLAN 的总 IP 数和已用/可用 IP,可以使用哪个 APIVLAN ID.

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

Thanks


要获取有关 VLAN 子网以及已用/可用 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. <[email protected] /cdn-cgi/l/email-protection>
"""
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 http://sldn.softlayer.com/reference/services/SoftLayer_Account/getNetworkVlans

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SoftLayer API 用于了解 VLAN 中的总 IP 和可用 IP 的相关文章

随机推荐