将pcap打印到远程服务器时出现Cat错误 [英] Cat error when printing pcap to remote server

查看:67
本文介绍了将pcap打印到远程服务器时出现Cat错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The script is as follows; When I run it manually, there is no problem but

   #!/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输出如下.可能是什么问题?

The sh -x output is as follows. What could be the problem?

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 在过滤器表达式之后(选项必须在表达式之前):

The original version had two problems, the stray . character before & at the end, and the -w - option to tcpdump being after the filter expression (options must come before the expression):

/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: ^

我还用 $()替换了反引号,即

I also replaced the backticks with $( ), which is cleaner in a couple of ways, and put double-quotes around the resulting filename (not strictly necessary, but good general practice for something that contains a command or variable substitution).

由于缺少空格,您在注释中尝试的版本还有一些其他问题: -Z username-s 0 而不是 -Z username -s 0 12345-w-而不是 12345 -w-.在shell语法中,空格是重要的分隔符,因此请小心不要添加或删除它们,除非您知道在该特定位置是安全的.在这里,删除选项与其参数之间的空格(例如 -Zusername -s0 而不是 -Z username -s 0 )是可以的,但是要删除参数和下一个选项之间的空格会引起混乱.

The versions you tried in the comments had a couple of additional problems due to missing spaces: -Z username-s 0 instead of -Z username -s 0 and 12345-w - instead of 12345 -w -. Spaces are important delimiters in shell syntax, so be careful not to add or remove them unless you know it's safe in that specific place. Here, it would've been ok to remove the spaces between an option and its argument (e.g. -Zusername -s0 instead of -Z username -s 0), but removing a space between an argument and the next option causes chaos.

这篇关于将pcap打印到远程服务器时出现Cat错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆