将 pcap 打印到远程服务器时出现 Cat 错误

Cat error when printing pcap to remote server

脚本如下;当我手动运行时,没有问题但是

   #!/bin/bash

/usr/bin/sudo /usr/sbin/tcpdump -i any -Z user -s 0 -v port xxx or xxx -w - | ssh root@xx.xx.xx.xx -C 'cat - > /opt/pcap/trace.`date +\%s`' .&

sleep 10
pkill tcpdump
exit 

sh -x 输出如下。可能是什么问题?

cat .: 是一个目录

这是更正后的版本:

/usr/bin/sudo /usr/sbin/tcpdump -i any -Z user -s 0 -w - -v port xxx or xxx | ssh root@xx.xx.xx.xx -C 'cat - > "/opt/pcap/trace.$(date +\%s)"' &

原始版本有两个问题,最后 & 之前的杂散 . 字符,以及 tcpdump-w - 选项在过滤器表达式之后(选项必须在表达式之前):

/usr/bin/sudo /usr/sbin/tcpdump -i any -Z user -s 0 -v port xxx or xxx -w - | ssh root@xx.xx.xx.xx -C 'cat - > /opt/pcap/trace.`date +\%s`' .&
                                                        problems here: ^^^^                                                       and here: ^

我还用 $( ) 替换了反引号,即 cleaner in a couple of ways,并在生成的文件名周围加上双引号(并非绝对必要,但对于包含命令或变量替换)。

您在评论中尝试的版本由于缺少 spaces 而存在一些其他问题:-Z username-s 0 而不是 -Z username -s 012345-w - 而不是 12345 -w -。空格是 shell 语法中的重要分隔符,因此请注意不要添加或删除它们,除非您知道它在特定位置是安全的。在这里,删除选项及其参数之间的 space 是可以的(例如 -Zusername -s0 而不是 -Z username -s 0),但删除参数之间的 space下一个选项会导致混乱。