我在运行脚本时遇到了一些问题。
这在命令行中完美运行:
ssh root@ip.add.re.ss /usr/sbin/tcpdump -i eth0 -w - | /usr/sbin/tcpdump -r - -w /home/cuckoo/cuckoo/storage/analyses/1/saveit.pcap
但是当我使用这个脚本时: #!/ usr / bin / env python
import sys
import os
import subprocess
cmd = []
remote_cmd = []
local_cmd = []
connect_cmd = []
outfile = None
try:
connect_cmd = str.split(os.environ["RTCPDUMP_CMD"], " ")
except:
connect_cmd = str.split("ssh root@fw", " ")
remote_cmd.extend(str.split("/usr/sbin/tcpdump -w -", " "))
local_cmd.extend(str.split("/usr/sbin/tcpdump -r -", " "))
for argument in xrange(1, len(sys.argv)):
if sys.argv[argument] == "-w":
outfile=sys.argv[argument+1]
sys.argv[argument] = None
sys.argv[argument+1] = None
if sys.argv[argument] == "-i":
remote_cmd.append(sys.argv[argument])
remote_cmd.append(sys.argv[argument+1])
sys.argv[argument] = None
sys.argv[argument+1] = None
if not sys.argv[argument] == None:
if " " in sys.argv[argument]:
local_cmd.append("'" + sys.argv[argument] + "'")
remote_cmd.append("'" + sys.argv[argument] + "'")
else:
local_cmd.append(sys.argv[argument])
remote_cmd.append(sys.argv[argument])
if not outfile == None:
local_cmd.insert(1, "-w")
local_cmd.insert(2, outfile)
cmd.extend(connect_cmd)
cmd.extend(remote_cmd)
cmd.append("|")
cmd.extend(local_cmd)
try:
subprocess.call(cmd)
except KeyboardInterrupt:
exit(0)
它会在远程主机上生成两个tcpdump进程,而第二个tcpdump由于非工作路径而无法保存。我在末尾添加了一个print cmd,并且传递给提示符的ssh命令完全相同(当运行脚本本身时,cuckoo在调用脚本时传递了大量选项。并且它在 - 之前得到-w - 我是eth0,但我测试了它,它也可以从命令行工作。
所以我彻底难倒,为什么本地的管道不能在脚本中工作但是它起作用了吗?
哦,这个剧本的功劳属于Michael Boman http://blog.michaelboman.org/2013/02/making-cuckoo-sniff-remotely.html
答案 0 :(得分:0)
所以我彻底难倒,为什么本地的管道不能在脚本中工作但是它起作用了吗?
因为管道由shell处理,而你没有运行shell。
如果您查看文档,请在Replacing Older Functions with the subprocess
Module下解释如何执行shell管道相同的操作。这是一个例子:
output=`dmesg | grep hda`
# becomes
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]
所以,按照你的意思:
cmd.extend(connect_cmd)
cmd.extend(remote_cmd)
try:
remote = subprocess.Popen(cmd, stdout=subprocess.PIPE)
local = subprocess.Popen(local_cmd, stdin=remote.stdout)
remote.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
local.communicate()
except KeyboardInterrupt:
exit(0)