Azure Resource Graph - Regex does not work in Python

Maciej 1 Reputation point
2022-09-15T09:36:45.857+00:00
# Import Azure Resource Graph library  
import os  
import azure.mgmt.resourcegraph as arg  
  
# Import specific methods and models from other libraries  
from azure.mgmt.resource import SubscriptionClient  
from azure.identity import AzureCliCredential  
  
# Wrap all the work in a function  
def getresources(strQuery):  
    # Get your credentials from Azure CLI (development only!) and get your subscription list  
    credential = AzureCliCredential()  
    subsClient = SubscriptionClient(credential)  
    subsRaw = []  
    for sub in subsClient.subscriptions.list():  
        subsRaw.append(sub.as_dict())  
    subsList = []  
    for sub in subsRaw:  
        subsList.append(sub.get('subscription_id'))  
  
    # Create Azure Resource Graph client and set options  
    argClient = arg.ResourceGraphClient(credential)  
    argQueryOptions = arg.models.QueryRequestOptions(result_format="objectArray")  
  
    # Create query  
    argQuery = arg.models.QueryRequest(subscriptions=subsList, query=strQuery, options=argQueryOptions)  
  
    # Run query  
    argResult = argClient.resources(argQuery)  
  
    return argResult  
  
  
  
def main():  
    result = getresources("""  
resources | where properties matches regex @'\b[0-9]\.\d{1,3}\.\d{1,3}\.\d{1,3}\b' or  
    properties matches regex @'\b1[1-9]\.\d{1,3}\.\d{1,3}\.\d{1,3}\b' or  
    properties matches regex @'\b[2-9][0-9]\.\d{1,3}\.\d{1,3}\.\d{1,3}\b' or  
    properties matches regex @'\b1[0-6][0-9]\.\d{1,3}\.\d{1,3}\.\d{1,3}\b' or  
    properties matches regex @'\b171\.\d{1,3}\.\d{1,3}\.\d{1,3}\b' or  
    properties matches regex @'\b172\.3[3-9]\.\d{1,3}\.\d{1,3}\b' or  
    properties matches regex @'\b172\.[4-9][0-9]\.\d{1,3}\.\d{1,3}\b' or  
    properties matches regex @'\b17[3-9]\.\d{1,3}\.\d{1,3}\.\d{1,3}\b' or  
    properties matches regex @'\b18[0-9]\.\d{1,3}\.\d{1,3}\.\d{1,3}\b' or  
    properties matches regex @'\b19[0-1]\.\d{1,3}\.\d{1,3}\.\d{1,3}\b' or  
    properties matches regex @'\b192\.[0-9]\.\d{1,3}\.\d{1,3}\b' or  
    properties matches regex @'\b192\.[1-9][0-9]\.\d{1,3}\.\d{1,3}\b' or  
    properties matches regex @'\b192\.1[0-5][0-9]\.\d{1,3}\.\d{1,3}\b' or  
    properties matches regex @'\b192\.16[0-7]\.\d{1,3}\.\d{1,3}\b' or  
    properties matches regex @'\b192\.169\.\d{1,3}\.\d{1,3}\b' or  
    properties matches regex @'\b192\.1[7-9][0-9]\.\d{1,3}\.\d{1,3}\b' or  
    properties matches regex @'\b192\.2[0-5][0-9]\.\d{1,3}\.\d{1,3}\b' or  
    properties matches regex @'\b19[3-9]\.\d{1,3}\.\d{1,3}\.\d{1,3}\b' or  
    properties matches regex @'\b2[0-5][0-9]\.\d{1,3}\.\d{1,3}\.\d{1,3}\b'  
    | where type !~ 'microsoft.network/networksecuritygroups'  
    |project  name, type, location, resourceGroup, subscriptionId, properties  
    """)  
   
    print(result)  
  
if __name__ == '__main__':  
    main()   

This code does not return anything but when I run it in Azure Portal I get the results I want.

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
38,546 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.