3

I'm using the Python SDK for Azure but I can see the documentation lacks of proper information and examples. I know that the Azure CLI is built on top of the Python SDK for Azure, therefore I was wondering if there's any way you could know what Python function is calling when doing for example: "az vm show".

In that way, I could easily build the command using the Azure CLI and transform it into Python code.

Thank you!

drycon43
  • 33
  • 2

2 Answers2

0

This is my workaround:

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute import ComputeManagementClient

Subscription_Id = "xxxxxxx"
Tenant_Id = "xxxxxxx"
Client_Id = "xxxxxxx"
Secret = "xxxxxxx"

credential = ServicePrincipalCredentials(
        client_id=Client_Id,
        secret=Secret,
        tenant=Tenant_Id
        )

client = ComputeManagementClient(credential, Subscription_Id)
vmDetails = client.virtual_machines.get(resource_group_name='xxxxxxx', vm_name='xxxxxxx')
print(vmDetails)
unknown
  • 6,778
  • 1
  • 5
  • 14
  • 1
    Hi Pamela, thank you for answer but it's not providing a real answer to my question. – drycon43 Aug 04 '20 at 08:38
  • @drycon43 There is no method to transform CLI into Python directly. You need to find the azure function of Python, and this [link](https://learn.microsoft.com/en-us/python/api/overview/azure/virtualmachines?view=azure-python) shows Azure virtual machine libraries. For more reference, see [here](https://learn.microsoft.com/en-us/python/api/overview/azure/?view=azure-python). – unknown Aug 04 '20 at 08:51
0

You can see the underlying API that issued by the Python Process.

To do that, you could use fiddler or any client application that can capture traffic.

For Fiddler you can download and install from here

You will have enable Decrypt HTTPS traffic as all the traffic to Azure through CLI is done through HTTPs. Refer this for more information

Fiddler on running will now act as a proxy and will capture all the network traffic.

However, you will NOT be able to run the Azure CLI directly with the Fiddler on (Proxy) in place. You will have to add the certificate of the Fiddler to that of CLI's.

The fiddler Certificate can be found at http://localhost:8888. You will have to export the certificate and convert it to PEM format. The exported format is in .CRT

You can locally do this like mentioned here or use online tool to do this example :https://www.sslshopper.com/ssl-converter.html

Once you have the PEM Certificate - append the contents of this to certificate file that the CLI uses :

C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\Lib\site-packages\certifi\cacert.pem

<Original cacert.pem>

-----BEGIN CERTIFICATE-----
<Your proxy's certificate here>
-----END CERTIFICATE-----

Now you will be able to intercept the traffic issued from a python process.

enter image description here

More information on this is documented here

Satya V
  • 3,811
  • 1
  • 6
  • 9
  • 1
    @drycon43 - The Python process issues API requests with the necessary headers and request body. The API endpoint would be the core and would be the value add. The above method will help you get the API. – Satya V Aug 05 '20 at 06:44
  • do you know a fix for the problem: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1125) – Rob Bowman Jul 19 '22 at 06:52