subprocess command fails
Cameron Simpson
cs at zip.com.au
Fri Feb 20 20:11:57 EST 2015
On 20Feb2015 15:30, Brad s <bcddd214 at gmail.com> wrote:
>I am trying to execute a subprocess, something done in my script a couple of times. But on the last one, it outputs an error I cannot find the solution to. The exact same command using the same files produced at the command line works just fine.
>
Hi Brad,
I have reordered your post in my quote for readability. It is best to ask your
question and give some background first, then list offending output with a
description of good output, then the code.
You write:
>The first command works on the command line:
>
>dnssec-signzone -e20180330000000 -p -t -g -k Ktest123.com.ksk.key -o test123.com test123.com.external Ktest123.com.zsk.key
>Verifying the zone using the following algorithms: RSASHA256.
>Zone fully signed:
>Algorithm: RSASHA256: KSKs: 1 active, 0 stand-by, 0 revoked
> ZSKs: 1 active, 0 stand-by, 0 revoked
>test123.com.external.signed
>Signatures generated: 9
>Signatures retained: 0
>Signatures dropped: 0
>Signatures successfully verified: 0
>Signatures unsuccessfully verified: 0
>Signing time in seconds: 0.010
>Signatures per second: 875.401
>Runtime in seconds: 0.013
Which is excellent.
Then you supply your code:
>sfmove = subprocess.call(['dnssec-signzone','-e',strftime('%Y%m%d%H', gmtime())+'0000','-p','-t','-g','-k',zcombined+'.ksk.key','-o',dname,dname+'.external',zcombined+'.zsk.key'])
I would start by pointing out that this is not identical to your shell command
line. FOr example, your shell command line supplied the -e optin as
"-e20180330000000", but your python code generates two strings: "-e",
"20180330000000".
It may not matter, but it is not identically worded and some commands are picky
about this kind of thing.
For debugging purposes I would do two things:
- compute the python command argument list as a separate list and print it out, example:
cmdargv = [ 'dnssec-signzone',
'-e', strftime('%Y%m%d%H', gmtime())+'0000',
'-p',
'-t',
'-g',
'-k', zcombined+'.ksk.key',
'-o', dname,
dname+'.external',
zcombined+'.zsk.key'
]
print("command = %r" % (cmdargv,))
sfmove = subprocess.call(cmdargv)
Note the use of %r to print the contents of the command clearly.
(BTW, "sfmove"? Surely "sign" or something.)
- hand run _exactly_ the command printed out by the print call above
If you run _exactly_ what your python comman did, you should be able to
reproduce the error. Then debug on the command line. Then fix the python code
accordingly.
>#cmd = "dnssec-signzone','-e',strftime('%Y%m%d%H', gmtime())+'0000','-p','-t','-g','-k','K'+dname+'.ksk.key','-o',dname,dname+'.external','K"+dname+'.zsk.key'
>#subprocess.check_call(shlex.split(cmd))
Remark: shlex.split is not a good way to make a command line unless it comand
from some input line obtained from a user. Stick with your current "construct a
list" approach: more robust.
I notice your code removes a bunch of files. Might it remove a necessary file
for the command?
Cheers,
Cameron Simpson <cs at zip.com.au>
More information about the Python-list
mailing list