0

I'm trying to call certutil from inside python. However I need to use quotation marks and have been unable to do so. My code is as follows:

import subprocess
output= subprocess.Popen(("certutil.exe", "-view", '-restrict "NotAfter <= now+30:00, NotAfter >= now+00:00"' ), stdout=subprocess.PIPE).stdout
for line in output:
    print(line)

output.close()

I thought the single quotes would allow me to use double quotes inside the string.

Also I have tried using double quotes with the escape character (\"), however I keep getting the same error:

Unknown arg: -restrict \\NotAfter\r\n'

For some reason it seems to be translating " into \\.

Can anyone give insight as to why and how to fix it?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Dorian
  • 1
  • 2

2 Answers2

0

I do not have Python in any version installed. But according to answer on How do I used 2 quotes in os.system? PYTHON and documentation of subprocess, subprocess handles requirement for double quotes on arguments with space automatically.

So you should need to use simply:

import subprocess
output= subprocess.Popen(("certutil.exe", "-view", "-restrict", "NotAfter <= now+30:00, NotAfter >= now+00:00" ), stdout=subprocess.PIPE).stdout
for line in output:
    print(line)

output.close()

And the command line used on calling certutil is:

certutil.exe -view -restrict "NotAfter <= now+30:00, NotAfter >= now+00:00"
Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
0
output=subprocess.Popen(("certutil.exe -view -restrict  \"NotAfter<=now+30:00,NotAfter>=now+00:00\"" ),stdout=subprocess.PIPE).stdout

This is what was needed. I was passing the command as a command with 2 args. What I should have been doing was passing it as one big command with 2 parameters.

Dorian
  • 1
  • 2