jia chang
jia chang

Reputation: 13

print out nsg with security rules using azure python network management module

I want to be able to use azure python network management module to print out exisiting NSG with security rules.

when I print out the result, I see { 'additional_properties': {}, 'id': '/subscriptions/1111-22222-33334--44444/resourceGroups/RG-US-e-Network/providers/Microsoft.Network/networkSecurityGroups/testnsg', 'name': 'NSG-E-E-BE', 'type': 'Microsoft.Network/networkSecurityGroups', 'location': 'eastus', 'tags': {}, 'etag': 'W/"a3d08ba5-3620-4ce3-88b5-48f859c5dbf5"', 'security_rules': [ < azure.mgmt.network.v2021_02_01.models._models_py3.SecurityRule object at 0x7f023e616c88 > , < azure.mgmt.network.v2021_02_01.models._models_py3.SecurityRule object at 0x7f023e616cc0 > ], 'default_security_rules': [ < azure.mgmt.network.v2021_02_01.models._models_py3.SecurityRule object at 0x7f023e616cf8 > , < azure.mgmt.network.v2021_02_01.models._models_py3.SecurityRule object at 0x7f023e616d30 > , < azure.mgmt.network.v2021_02_01.models._models_py3.SecurityRule object at 0x7f023e616da0 > , < azure.mgmt.network.v2021_02_01.models._models_py3.SecurityRule object at 0x7f023e616dd8 > , < azure.mgmt.network.v2021_02_01.models._models_py3.SecurityRule object at 0x7f023e616e10 > , < azure.mgmt.network.v2021_02_01.models._models_py3.SecurityRule object at 0x7f023e616e48 > ], 'network_interfaces': None, 'subnets': None, 'flow_logs': None, 'resource_guid': '8a360147-xxxx-46b8-b8be-xxxxxx', 'provisioning_state': 'Succeeded' }

The security rule section doesn't list out rules but this:

'security_rules': [ < azure.mgmt.network.v2021_02_01.models._models_py3.SecurityRule object at 0x7f023e616c88 > , < azure.mgmt.network.v2021_02_01.models._models_py3.SecurityRule object at 0x7f023e616cc0 > ],

maybe my print function is incorrect. what did I do wrong here? below is my simple code:


credential = AzureCliCredential()
#credential = DefaultAzureCredential
network_client = NetworkManagementClient(credential, subscription_id)

resource_group_name = "RG-EUS-ECS-Network"
nsg_name = "testnsg"

nsg_params = NetworkSecurityGroup(id= "testnsg", location="westus", tags={ "name" : "testnsg" })

nsglist = network_client.network_security_groups.list(resource_group_name)

pageobject=nsglist.by_page(continuation_token=None)

for page in pageobject:
    for i in page:
        print (i)

Upvotes: 1

Views: 742

Answers (1)

Ansuman Bal
Ansuman Bal

Reputation: 11411

The script you are using is printing the Network Security Groups Located in a resource group. To print the Network security rules for a NSG you have to use the below code:

from azure.identity import AzureCliCredential
from azure.mgmt.network import NetworkManagementClient
credential = AzureCliCredential()
subscription_id = "Your subid"
#credential = DefaultAzureCredential
network_client = NetworkManagementClient(credential, subscription_id)
resource_group_name = "resourcegroupname"
nsg_name = "nsgname"
nsgruleslist = network_client.security_rules.list(resource_group_name,nsg_name)
pageobject1=nsgruleslist.by_page(continuation_token=None)
for page in pageobject1:
    for j in page:
        print(j)

Output:

Enter image description here

enter image description here

The above displays all the security rules set on an NSG as shown in the image.

Upvotes: 1

Related Questions