shell script를 통해서 tcpdump 출력을 변경하고 싶습니다.

s9yirl의 이미지

아래와 같은 출력해서 특정 라인만 뽑아서 출력하지 않고,
전체를 출력하는 동시에

0x0000: 38ea a717 df3c 14fe b5da e10e 0800 4500

에서 :는 삭제하고 각 4자리 숫자를 2자리 사이마다 공백을 넣고 싶어요

0x0000 38 ea a7 17 df 3c 14 fe b5 da e1 0e 08 00 45 00

이렇게요.

sed랑 awk, if를 써서 해보려고 했는데 너무 어렵네요.ㅠㅠ

도움 부탁드립니다.

[root@localhost ]# tcpdump -l -s 1700 -xx -A -i eth3 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth3, link-type EN10MB (Ethernet), capture size 1700 bytes
17:26:13.821676 IP 20.4.1.31 > 72.0.0.6: ICMP echo request, id 48478, seq 1, length 64
0x0000: 38ea a717 df3c 14fe b5da e10e 0800 4500
0x0010: 0054 0000 4000 4001 dd80 1404 011f 4800
0x0020: 0006 0800 f560 bd5e 0001 a559 0059 a889
0x0030: 0c00 0809 0a0b 0c0d 0e0f 1011 1213 1415
0x0040: 1617 1819 1a1b 1c1d 1e1f 2021 2223 2425
0x0050: 2627 2829 2a2b 2c2d 2e2f 3031 3233 3435
0x0060: 3637
17:26:14.820866 IP 20.4.1.31 > 72.0.0.6: ICMP echo request, id 48478, seq 2, length 64
0x0000: 38ea a717 df3c 14fe b5da e10e 0800 4500
0x0010: 0054 0000 4000 4001 dd80 1404 011f 4800
0x0020: 0006 0800 1c63 bd5e 0002 a659 0059 8086
0x0030: 0c00 0809 0a0b 0c0d 0e0f 1011 1213 1415
0x0040: 1617 1819 1a1b 1c1d 1e1f 2021 2223 2425
0x0050: 2627 2829 2a2b 2c2d 2e2f 3031 3233 3435
0x0060: 3637
17:26:15.829828 IP 20.4.1.31 > 72.0.0.6: ICMP echo request, id 48478, seq 3, length 64
0x0000: 38ea a717 df3c 14fe b5da e10e 0800 4500
0x0010: 0054 0000 4000 4001 dd80 1404 011f 4800
0x0020: 0006 0800 193f bd5e 0003 a759 0059 82a9
0x0030: 0c00 0809 0a0b 0c0d 0e0f 1011 1213 1415
0x0040: 1617 1819 1a1b 1c1d 1e1f 2021 2223 2425
0x0050: 2627 2829 2a2b 2c2d 2e2f 3031 3233 3435
0x0060: 3637

ymir의 이미지

$ sudo tcpdump -l -s 1700 -xx -A -i enp0s3 icmp | \
sed -e '/0x[[:xdigit:]]*:/s/\s\([[:xdigit:]]\{2\}\)\([[:xdigit:]]\{2\}\)/ \1 \2/g;s/:  / /g'
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on enp0s3, link-type EN10MB (Ethernet), capture size 1700 bytes
09:11:10.306062 IP asgaard > 192.168.0.1: ICMP echo request, id 2651, seq 1, length 64
        0x0000 90 9f 33 77 a8 18 08 00 27 ef 19 70 08 00 45 00
        0x0010 00 54 35 29 40 00 40 01 83 30 c0 a8 00 fe c0 a8
        0x0020 00 01 08 00 66 65 0a 5b 00 01 1e 37 01 59 78 ab
        0x0030 04 00 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15
        0x0040 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25
        0x0050 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35
        0x0060 36 37
09:11:10.307407 IP 192.168.0.1 > asgaard: ICMP echo reply, id 2651, seq 1, length 64
        0x0000 08 00 27 ef 19 70 90 9f 33 77 a8 18 08 00 45 00
        0x0010 00 54 dc ff 00 00 40 01 1b 5a c0 a8 00 01 c0 a8
        0x0020 00 fe 00 00 6e 65 0a 5b 00 01 1e 37 01 59 78 ab
        0x0030 04 00 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15
        0x0040 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25
        0x0050 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35
        0x0060 36 37

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

s9yirl의 이미지

sed 만으로 가능했네요~~ 감사합니다~ :)

백연구원의 이미지

계속 들여다봐도 믿을 수 없는 코드네요.. 실례가 안되면 \s(backslash s) 의 의미를 배울 수 있을까요? \1 \2 이러한 것의 존재도 난생 처음 봤습니다.

그저 감탄할 뿐입니다.


소곤소곤

ymir의 이미지

\s 가 perl regex 라고 한 부분은 그냥 기억에 있던 대로 적은 거였는데..
아무래도 뭔가 이상한 것 같아서 다시 확인해 보니, 잘못된 내용이어서 바로잡습니다.

\ 뒤에 sequence 를 넣는 것은 regex 의 shorthand character classes 에 정의되어 있는 내용입니다.

http://www.regular-expressions.info/shorthand.html

간단히 regex 매뉴얼 볼 때 주로 perlre 를 보다 보니.. 기억이 그렇게 굳어졌나 봅니다. ;;

POSIX 에서는 character class 에 bracket expression 을 사용하구요.

http://www.regular-expressions.info/posixbrackets.html

sed 에 대한 내용은 아래 매뉴얼 5.5, 5.6 을 보시면 될 것 같네요.

https://www.gnu.org/software/sed/manual/sed.txt
5.5 Character Classes and Bracket Expressions
5.6 regular expression extensions

두번째 \1 \2 는 sed capture group 을 reference 하는 겁니다.
\(pattern\) 과 같이 pattern 을 괄호로 감싸고, 거기서 매치된 pattern 을, \1 과 같이 순서대로 번호를 붙여서 참조하는 거죠.

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

백연구원의 이미지

잘 배워 갑니다. 너무 감사드립니다.

오늘따라 광선검이 더 빛나는 것 같네요 :-)


소곤소곤

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.