IBM Cloud Global

Cloud Global

Our mission is to provide clients with an online user community of industry peers and IBM experts, to exchange tips and tricks, best practices, and product knowledge. We hope the information you find here helps you maximize the value of your IBM Cloud solutions.

 View Only

Automate Network Insights: Use Python to Retrieve VLAN, and Router Details for Your IBM Cloud Classic Account IP

By Lavisha Bhatia posted Tue October 01, 2024 03:24 AM

  

Crack the code to your network! 🖥️💡

In this blog, we’re diving into some Python magic to fetch VLAN, Gateway and Router details for your IP from IBM Cloud Classic account – because who says network management can’t be automated (and fun)? 

Here is the code :

import SoftLayer
import pprint
import os  # For accessing environment variables
import sys

__author__ = "Lavisha"
__version__ = "1.0"
__description__ = ("A Python class to fetch Vlan and Router details.")

# Fetch username and API key from environment variables
username = os.getenv("SL_USERNAME")
password = os.getenv("SL_API_KEY")

# Ensure both environment variables are available
if not username or not password:
    print("Error: Please ensure SL_USERNAME and SL_API_KEY environment variables are set.")
    sys.exit(1)

# Create client using environment variables
client = SoftLayer.create_client_from_env(username, password)
IP_Address = input("Please enter IP Address: ")

# Customizable client creation
"""if you wish to customize code with your username & API_Key, uncomment following line
client = SoftLayer.create_client_from_env(username='accountID_IBMEmailID', api_key='xxxx')
Note: Username details can found on Cloud Portal at following path: Access(IAM) > Users > Search your name > Check Username under VPN password section. 
Note: Generate Api_Key under section "Classic Infrastructure API Keys"
"""

class GetIPDetails:
    """A Class to fetch IP details"""
    def get_Ip_info(IP_Address):
        resp = client.call('Account', 'getObject')
        pprint.pprint(resp['companyName'])
        mgr = SoftLayer.NetworkManager(client)
        return mgr.ip_lookup(IP_Address)


class GetSubnetInfo:
    """A Class to fetch Subnet details"""
    def get_subnet_info(subnet_id):
        mgr = SoftLayer.NetworkManager(client)
        return mgr.get_subnet(subnet_id)


# Usage
if __name__ == "__main__":
    IP_details = GetIPDetails.get_Ip_info(IP_Address)

    if IP_details and 'subnetId' in IP_details:
        subnet_id = IP_details.get('subnetId')
        subnet_details = GetSubnetInfo.get_subnet_info(subnet_id)

    print("IP_Address:", IP_details.get('ipAddress'))
    print("Address_Space:", subnet_details.get('addressSpace'))
    print("Is it Broadcast_Address:", IP_details.get('isBroadcast'))
    print("Is it Gateway_Address:", IP_details.get('isGateway'))
    print("Is it Network_Address:", IP_details.get('isNetwork'))
    print("Broadcast_Address:", subnet_details.get('broadcastAddress'))
    print("Gateway_Address:", subnet_details.get('gateway'))
    print("Subnet_ID:", IP_details.get('subnet', {}).get('id', {}))
    print("Net_Mask:", IP_details.get('subnet', {}).get('netmask', {}))
    print("CIDR:", subnet_details.get('cidr'))
    print("Subnet_Type:", IP_details.get('subnet', {}).get('subnetType', {}))
    print("Router & Vlan details:", subnet_details.get('networkVlan', {}).get('fullyQualifiedName'))
    print("Network_Vlan_ID:", IP_details.get('subnet', {}).get('networkVlanId', {}))
    print("Router hostname:", subnet_details.get('networkVlan', {}).get('primaryRouter', {}).get('hostname'))
    print("Modify_Date:", IP_details.get('subnet', {}).get('modifyDate', {}))

Usage example:

You need to set the environment variables SL_USERNAME and SL_API_KEY on your system. Here’s how to do it depending on your OS:

  • For Linux/macOS:
    You can set the environment variables in your shell (e.g., bash, zsh):

    export SL_USERNAME="your_username"
    export SL_API_KEY="your_api_key"
    

To make these variables persist across sessions, you can add the lines above to your shell configuration file (~/.bashrc, ~/.zshrc, etc.).

  • For Windows:
    You can set environment variables in the Command Prompt or PowerShell:

    setx SL_USERNAME "your_username"
    setx SL_API_KEY "your_api_key"
    

After setting the variables, restart your terminal or script to have them take effect.
Once setting the variables is done, you just need to run command : <python3 Get_IP_Details.py>


Simply provide the IP address you need details for, and voilà! You'll receive all the information you need without the hassle of searching through the portal.

0 comments
5 views

Permalink