Programmatic connection to third party database using "runas"?

37 Views Asked by At

My work laptop is connected in the corporate network of company A, and my user account is "A/username". I have been provisioned access in the network of company B, with a user account "B/username", to access their databases.

We use Microsoft SQL Server Management Studio, and the "trick" is to execute a command to run this as an alias:

C:\Windows\System32\runas.exe /netonly /user:**user@company_B.com** "C:\Program Files (x86)\Microsoft SQL Server Management Studio 18\Common7\IDE\Ssms.exe"

This leads to the popup of a prompt for me to enter my password for my company B account, and then the UI for MS SQL Server Management Studio starts.

On this, if I click on Connect > Database Engine, the only thing I need to add the server (host) of the company B DB: "sql-server.company_b.com", and I can access the databases/tables, even though the authentication method (Windows Authentication) has a grayed out username for company A - this is circumvented by the runas part in the alias.

Is there a way to emulate this whole behavior programmatically (preferably in Python) with pyodbc or pymssql? I have been trying to populate the URL.connection_string using sqlalchemy but I always get an error of authentication when I try to connect to the company B's server without using the alias to SSMS.exe ...

0

There are 0 best solutions below