Search code examples
pythonshellcode

Use a grep command in python


There is a command like the following:

for i in $(objdump -d binary |grep "^ " |cut -f2); do echo -n '\x'$i; done;echo

How can I use this command in my python code and then deliver the result of the hexdump (print) to the console.

    objdump = "$(objdump -d " + str(_arg_name) + "| grep '^ ' |cut -f2);" + " do echo -n $i; done; echo"
    result = os.popen(objdump).read()
    result = result.replace('"','')
    print(result)

Solution

  • You can use the following code:

        g1 = "grep '^ '"
        g2 = "cut -f2"
    
        objdump = "objdump -d " + str(binary) + "|" + g1 + "|" + g2 
        result = os.popen(objdump).read()
        result = result.replace('\n','')
        result = result.replace(' ','')
        length = len(result) - 1
        result = [r'\x' + result[i:i + 2] for i in range(0, length, 2)]
        print(result)