Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python run bash command get bad result

Tags:

python

bash

cmd

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:
0cc175b9c0f1b6a831c399e269772661

Where I did mistake?

like image 233
pierre tautou Avatar asked Nov 21 '25 07:11

pierre tautou


1 Answers

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)
like image 188
srgerg Avatar answered Nov 23 '25 23:11

srgerg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!