因为工作任务,需要写一个Tcpdump 来显示包的一个脚本。
脚本内容如下:
#!/usr/local/ActiveTcl/bin/tclsh
set package [ exec tcpdump -i eth0 -c 2]
puts $package
# end
在Linux环境下执行结果如下:
root@Server:~/wendh/test# ./tt2.tcl
15:35:27.343648 IP 11.1.1.2 > 22.1.1.2: ICMP echo request, id 60437, seq 32, length 64
15:35:27.343657 IP 22.1.1.2 > 11.1.1.2: ICMP echo reply, id 60437, seq 32, length 64
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
/etc/host.conf: line 1: bad command `Server'
2 packets captured
4 packets received by filter
0 packets dropped by kernel
while executing
"exec tcpdump -i eth0 -c 2"
invoked from within
"set a [exec tcpdump -i eth0 -c 2]"
(file "./tt2.tcl" line 3)
root@Server:~/wendh/test#
而在Tcl环境下执行结果如下:
root@Server:~/wendh/test# tclsh
% source tt2.tcl
15:37:16.344411 IP 11.1.1.2 > 22.1.1.2: ICMP echo request, id 60437, seq 141, length 64
15:37:16.344420 IP 22.1.1.2 > 11.1.1.2: ICMP echo reply, id 60437, seq 141, length 64
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
/etc/host.conf: line 1: bad command `Server'
2 packets captured
4 packets received by filter
0 packets dropped by kernel
% exit
root@Server:~/wendh/test#
想了很长时间。。也不明白为什么会这样。。会抛出异常。
经朋友提示。。将脚本修改了一下。如下:
#! /usr/local/ActiveTcl/bin/tclsh
catch {exec tcpdump -i eth0 -c 2 } package
puts $package
这样在Linux环境下就不会出现异常。。
如果各位有谁知道为什么会这样。。请给我留言!!!谢谢!!
本文介绍了一个使用Tcl脚本调用Tcpdump捕获数据包的案例。作者最初尝试直接通过exec执行Tcpdump命令并读取其输出,但在Linux环境下遇到了异常。通过调整脚本使用catch来处理可能的错误情况,成功解决了问题。
&spm=1001.2101.3001.5002&articleId=2777006&d=1&t=3&u=f0f8b3250d8a4f0e8cb41dadd8ed137c)
252

被折叠的 条评论
为什么被折叠?



