bradley plater
bradley plater

Reputation: 1273

Using Python to execute cmd commands

Hello i have been playing around with python recently and have been trying to learn how to control external peripherals and i/o ports on my laptop.

I have been trying to disable USB ports and disable my network adapter. However when i run my program it does not work. The code does not have a specific syntax error but when it is ran nothing happens.

import subprocess


def main():

    print("PROGRAM STARTED")
    subprocess.call(["runas", "/user:Administrator", "cmd.exe /c netsh interface set interface '*' admin=disable"])

    print("Program Exited")


if __name__ == "__main__":
    main()

Upvotes: 2

Views: 1898

Answers (2)

bradley plater
bradley plater

Reputation: 1273

I found the issue with the code. to start with i was using the subprocess.call function however trying to run the program with Administrator through python do it through command prompt and use this line of code instead

subprocess.run(["powershell","Disable-NetAdapter -Name '*'"])

Note* Yes i changed from cmd to powershell this is because the command was easier to use.

Upvotes: 0

Sagar Ruchandani
Sagar Ruchandani

Reputation: 95

I think you should try to run such commands as admin in windows. This might help: https://social.technet.microsoft.com/Forums/windows/en-US/05cce5f6-3c3a-4bb8-8b72-8c1ce4b5eff1/how-to-run-a-program-as-adminitrator-via-the-command-line?forum=w7itproappcompat

You can also modify your command to print the output in stdout to debug easily.
print subprocess.check_output(['runas','/user:Bradley', "cmd.exe /c netsh interface set interface '*' admin=disable")

Upvotes: 1

Related Questions