因为工作任务,需要写一个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环境下就不会出现异常。。
如果各位有谁知道为什么会这样。。请给我留言!!!谢谢!!