【发布时间】:2017-02-28 05:13:23
【问题描述】:
我正在执行以下子进程...
p.call(["./hex2raw", "<", "exploit4.txt", "|", "./rtarget"])
...它挂了。
但是如果我执行kmwe236@kmwe236:~/CS485/prog3/target26$ ./hex2raw < exploit4.txt | ./rtarget 那么它执行得很好。使用输入或管道操作符有什么问题吗?
我也试过sp.call(["./hex2raw", "<", "exploit4.txt", "|", "./rtarget"], shell=True)
整个代码如下所示UPDATED WITH SUGGESTIONS
import subprocess as sp
import pdb
for i in range(4201265,4201323):
pdb.set_trace()
d = hex(i)[2:]
output = " "
for i in range(len(d),0,-2):
output = output + d[i-2:i] + " "
out_buffer = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" + output + "00 00 00 00"
text_file = open("exploit4.txt", "w")
text_file.write("%s" % out_buffer)
# sp.call(["./hex2raw", "<", "exploit4.txt", "|", "./rtarget"], shell=True)
with open("exploit4.txt") as inhandle:
p = sp.Popen("./hex2raw",stdin=inhandle,stdout=sp.PIPE)
p2 = sp.Popen("./rtarget",stdin=p.stdout,stdout=sp.PIPE)
[output,error] = p2.communicate()
我得到一个错误是
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 8] Exec format error
调试后发生在触发子进程调用p = sp.Popen("./hex2raw",stdin=inhandle,stdout=sp.PIPE)
【问题讨论】:
-
写完后别忘了关闭文件。
-
另外,作为一般规则,不要将问题中的代码更新为最佳答案,否则您的问题将变得毫无意义。
标签: python subprocess