5

I am writing a code in python in which I need to get the internet traffic by software's name. It's required of me to use the cmd command netstat -nb, command which requires elevation. I have to keep it simple, something of one line or so, no long batch or powershell scripts. It's preferable if I use only the subprocess python library.

I have got two lines of code that work halfway of what I need:

subprocess.check_output('powershell Start-Process netstat -ArgumentList "-nb" -Verb "runAs"', stdout=subprocess.PIPE, shell=True)

The problem in this one is that a new window it's opened and all the data I need is lost. Maybe there's a way of not opening another window or saving the output from the new window?

subprocess.check_output('powershell Invoke-Command {cmd.exe -ArgumentList "/c netstat -nb"}', stdout=subprocess.PIPE, shell=True)

This one I have the output in the same window but I don't have elevation so I don't get any results... Maybe there is a way of getting elevation without opening a new window or so?

Thank you for your help, hope my question was clear enough.

Pedro Peck
  • 51
  • 1
  • 4
  • Why are you using powershell when you can just call those commands directly? – Maximilian Burszley Jun 22 '17 at 18:46
  • 2
    Why not have Python elevate itself instead of having PowerShell do that for you? Check out [this](https://stackoverflow.com/a/41930586/3245749) answer to another SO question for details on that. – TheMadTechnician Jun 22 '17 at 18:52
  • Have you had a look at the `RUNAS` command? – lit Jun 22 '17 at 20:22
  • Thank you for all your answers. With the function `ExecuteShellW` or any other of the kind I cannot save the output from the cmd command and that's why I need to use subprocess. And in order to use runas I need to insert the user and then the password, information that I won't be getting and even if I use it in my computer it doesn't works. What I need is to run silently(if possible) the `netstat -nb` command and save the output into a variable inside my python script. Sorry if something wasn't clear enough at first. – Pedro Peck Jun 23 '17 at 12:34
  • are you running your python script as an admin? – Caleb Seelhoff Jun 30 '17 at 16:01
  • I can't imagine why you're not running an administrative script under any condition other than elevated privileges, but one approach which might work (assuming you can't perform the elevation at the Python step) is to create a shortcut for `CMD /C netstat -nb` and assign the privileged state to that shortcut. You could then call that shortcut from your program. This is a variation of the third technique mentioned at [https://www.bleepingcomputer.com/tutorials/windows-elevated-command-prompt/] – Steven K. Mariner Aug 05 '17 at 00:19

1 Answers1

1

Create a batch file to perform the task with captured output to a temp file:

[donetstat.bat]
netstat -nb > ".\donetstat.tmp"

Then execute that in your program:

[yourprogram.py]
subprocess.check_output('powershell Start-Process cmd -ArgumentList "/c ".\donetstat.tmp" -Verb "runAs"', stdout=subprocess.PIPE, shell=True)

It would probably be a bit more bullet-resistent to get the TEMP environment variable and use it for a fully-qualified tempfile location:

netstat -nb > "%TEMP%.\donetstat.tmp"

And then get do the same in your Python script.

Once you've created the tempfile, you should be able to process it in Python.

If this needs to be durable with multiple worker processes, add some code to ensure you have a unique tempfile for each process.