I am utilizing subprocess in order to grab the hexdump for a .tgz file as I require the hex string. The only problem is, hexdump is throwing a bad format error, but only when the command is issues through subprocess. I believe I have escaped everything correctly, but I can't figure out why I am not getting my intended output:
def package_plugin():
plugin_hex = subprocess.run(["hexdump", "-v", "-e", "'1/1 \"\\\\x%%02x\"'", "package.tgz"])
This results in an error: hexdump: "'1/1 "''x%%02x"'": bad format. However, if I just run the command straight in the terminal I receive the expected output of a hexstring with the '\x' separating the hex.
How should I be running this to store the output in a Python variable? Is my command being mangled somehow and hence not executing correctly? Any advice is appreciated. Thanks
EDIT: I should add that when entering in the terminal the command is hexdump -v -e '1/1 "\\x%02x"'
I am not sure why the extra '%' sign is shown in the error as it should be interpreting as a single % sign.
Nevermind. I couldn't figure this out but I solved it with hexlify:
def package_plugin():
plugin_hex = ""
with open('plugin.tgz', 'rb') as f:
for chunk in iter(lambda: f.read(32), b''):
plugin_hex += binascii.hexlify(chunk).decode("utf-8")
formatted_hex = '\\x' + '\\x'.join(plugin_hex[i:i+2] for i in range(0, len(plugin_hex), 2))