SoftLayer API to know and total and available IPs

2019-03-06 15:25发布

问题:

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

Hello,

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

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.

Thanks

回答1:

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

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)

References: SoftLayer_Account::getNetworkVlans



回答2:

Please, try the following:

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

Or,

if you want this request retrieves the VLAN associated with an IP address via the IP's associated subnet:

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"
  ]
}

Reference: getVlanForIpAddress

Also you can see the meaning of data displayed in 'Subnet' section of above request, i.e.:

totalIpAddresses: The number of IP addresses contained within this subnet.

usableIpAddressCount: The number of IP addresses that can be addressed within this subnet.