SoftLayer API,用于了解 VLAN 中的总 IP 和可用 IP [英] SoftLayer API to know and total and available IPs in a VLAN

查看:12
本文介绍了SoftLayer API,用于了解 VLAN 中的总 IP 和可用 IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

你好

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

Which API can be used to know the total IPs and the used/usable IPs of a VLAN if I know the VLAN ID.

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

One way I could figure out is I can get subnets of a VLAN and then in subnet details I can see total and usable IPs with "totalIpAddresses,usableIpAddressCount" attributes . But then I will have to get sum of total and usable IPs for a VLAN since a VLAN has multiple subnets. Not sure if this is the correct way.

谢谢

推荐答案

要获取有关 vlan 子网的信息以及已使用/可用 IP 总数,请尝试以下 Python 脚本.

To get the information for the vlan about its subnets with the total used/usable IPs, try the following Python Script.

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

This script will help to get the exact number of free slots from subnets inside the 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

这篇关于SoftLayer API,用于了解 VLAN 中的总 IP 和可用 IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆