从文件 shell 脚本中获取子字符串 [英] get a substring from a file shell script

查看:42
本文介绍了从文件 shell 脚本中获取子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这个 shell 脚本的一些帮助.我有一个变量,代表一个 IP/TCP 标头.我需要通过选择的标头过滤流量捕获.

I need a little help with this shell script. I have a variable, represents a IP/TCP header. I need filter a traffic capture by the header selected.

> var=ttl 128
> 
> tcpdump -Xvv -n -i eth0 -c 300 > capture.txt 2>/dev/null
> 
> grep -i "$var" capture.txt > resultGrep.txt

tcpdump 命令的结果是这样的

The result of the tcpdump command is some like this

15:29:18.164566 IP (tos 0x0, ttl 1, id 2394, offset 0, flags [none], proto UDP (17), length 125)
    10.0.0.155.58363 > 239.255.255.254.1900: UDP, length 97
    0x0000:  4600 0024 0000 0000 0102 3ad3 0a00 0000  F..$......:.....
    0x0010:  e000 0001 9404 0000 1101 ebfe 0000 0000  ................
    0x0020:  0300 0000 0000 0000 0000 0000 0000       ..............
15:29:18.164566 IP (tos 0x0, ttl 128, id 2394, offset 0, flags [none], proto UDP (17), length 125)
    10.0.0.131.58363 > 239.255.255.250.1900: UDP, length 97
    0x0000:  4600 0024 0000 0000 0102 3ad3 0a00 0000  F..$......:.....
    0x0010:  e000 0001 9404 0000 1101 ebfe 0000 0000  ................
    0x0020:  0300 0000 0000 0000 0000 0000 0000       ..............

我需要有ip地址源和ip地址目标,在示例中输出结果必须是

I need have ip address source and ip address destination, in the example the output result must be

10.0.0.131.58363 > 239.255.255.250.1900

推荐答案

尝试在 tcpdump 上的 Unix 管道 中直接执行此操作:

Try doing this directly in a Unix pipe over tcpdump :

tcpdump -Xvv -n -i eth0 -c 300 |
grep -oP "d{1,3}.d{1,3}.d{1,3}.d{1,3}.d{1,5}s+>s+d{1,3}.d{1,3}.d{1,3}.d{1,3}.d{1,5}"

这是坚如磐石;)

这篇关于从文件 shell 脚本中获取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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