0

I'm trying to use os.system to use the taskkill command in command prompt. ill gut out the only part im having trouble with:

os.system('taskkill /s %s /u CORP\Administrator /p CLARiiON! /pid AxAuto.exe'%(connection[i]))

The variable connection[i] is just an IP Address of a remote computer on the same network. I can run this command straight from the command prompt locally and just directly input the IP and I know for a fact it will work, But running the command through Python in this format returns "> was unexpected at this time." Am I making a silly formatting mistake in this line of code? the error can be seen below: "> was unexpected at this time.


EDIT: I've also been told to use the Subprocess module. i tried the snippet below:

command="taskkill /s %s /u CORP\Administrator /p CLARiiON! /im AxAuto.exe"%(connection[i]))
subprocess.Popen(command, stdout= subprocess.PIPE, stdin = subprocess.PIPE, stderr=subprocess.PIPE)

It doesnt fail in the script but it also doesnt kill the process.

bladexeon
  • 696
  • 3
  • 10
  • 31
  • 1
    can you provide the exact error you're getting? Is the error a Windows error or a Python error? Also you may want to use the ``subprocess`` module instead of ``os.system``. – notorious.no Apr 23 '15 at 13:29
  • @notorious the error I get is shown in the Python script window, there is no trace back it only says "> was unexpected at this time." when it executes the command – bladexeon Apr 23 '15 at 13:32
  • @bladexeon I hope you are aware that you have posted the password in your python code! ;-) – ρss Apr 23 '15 at 13:35
  • The `/pid` argument should be an integer value. You may use `/img` for an image name of the process. https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/taskkill.mspx?mfr=true – ρss Apr 23 '15 at 13:40
  • @pss But I have used the name of the process in a normal Windows cmd prompt and it worked saying "Process xxx has been killed" or something like that so I know this will work – bladexeon Apr 23 '15 at 13:41

1 Answers1

1

Try something like the code below:

from subprocess import call

call(['taskkill', '/s', connection[i], '/u', 'CORP\Administrator', '/py',
      'CLARiiON!', '/pid', 'AxAuto.exe'])
martineau
  • 119,623
  • 25
  • 170
  • 301
notorious.no
  • 4,919
  • 3
  • 20
  • 34
  • This one gave me: `raise TypeError("This object does not support enumeration")` – bladexeon Apr 23 '15 at 14:03
  • This may be caused by ``connection``. What do you get when you print ``connection[i]`` – notorious.no Apr 23 '15 at 14:11
  • Aha! gives me this: `<_wmi_namespace: ,COMObject >>` this coming from a different part of the code...also being ssomething i have no idea how to fix – bladexeon Apr 23 '15 at 14:14
  • I don't think you need to "fix" it, you just need to figure out how to get the IP address from that object or even if it's possible. – notorious.no Apr 23 '15 at 14:22
  • I figured out the error I was using a `WMI` connection variable instead of the raw IP variable, and It did work with Subprocess. Thanks Much! – bladexeon Apr 23 '15 at 14:24