开发者

python run bash command get bad result

Hi I'm trying to run this bash cmd on python 3.2. Here is the python code:

message = '\\x61'
shell_command = "echo -n -e '" + message + "' | md5"
print(shell_command)
event = Popen(shell_command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
print(event.communicate())

this gave me next result:

echo -n -e '\x61' | md5

(b'713b2a82dc713ef273502c00787f9417\n', None)

But when I run this printed cmd in bash, I get different result:开发者_Go百科

0cc175b9c0f1b6a831c399e269772661

Where I did mistake?


The key to this problem is when you say:

But when I run this printed cmd in bash...

The Popen function of the subprocess module does not necessarily use bash, it may use some other shell such as /bin/sh which will not necessarily handle the echo command identically to bash. On my system running the command in bash produces the same result as you get:

$ echo -n -e '\x61' | md5sum
0cc175b9c0f1b6a831c399e269772661  -

But if I run the command in /bin/sh I get:

$ echo -n -e '\x61' | md5sum
20b5b5ca564e98e1fadc00ebdc82ed63  -

This is because /bin/sh on my system doesn't understand the -e option nor does it understand the \x escape sequence.

If I run your code in python I get the same result as if I'd used /bin/sh:

>>> cmd = "echo -n -e '\\x61' | md5sum"
>>> event = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)
>>> print event.communicate()
('20b5b5ca564e98e1fadc00ebdc82ed63  -\n', None)


You dont need to use echo to pass data. You can do it directly with python, i.e.:

Popen('/usr/bin/md5sum', shell=False, stdin=PIPE).communicate('\x61')


From the docs:

communicate() returns a tuple (stdoutdata, stderrdata).

That matches up with the tuple you got back:

(b'713b2a82dc713ef273502c00787f9417\n', None)

To access just the standard output (stdoutdata), you want element 0 of that tuple:

print(event.communicate()[0])


This would do the trick:

>>> p=Popen('echo -n \x61 |md5sum',shell=True,stdout=PIPE)
>>> p.communicate()
(b'0cc175b9c0f1b6a831c399e269772661  -\n', None)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜