代码之家  ›  专栏  ›  技术社区  ›  Mark

在bash中提取HTTP内容不适用于nc输出

  •  0
  • Mark  · 技术社区  · 7 年前

    假设我有一个HTTP响应:

    POST / HTTP/1.1
    Content-Type: text/plain;charset=UTF-8
    Content-Length: 5
    Connection: Keep-Alive
    Accept-Encoding: gzip
    Accept-Language: en,*
    User-Agent: Mozilla/5.0
    Host: 127.0.0.1:55764
    
    Hello
    

    我只对内容感兴趣(“你好”)。我发现如果文本是从文件馈送的,则此命令有效:

    cat data.txt | tr '\n' '#' | sed "s/.*##//" | tr '#' '\n'
    Hello
    

    其中data.txt包含上述文本。 nc :

    #!/bin/bash
    while true
    do
      echo -e "HTTP/1.1 200 OK\n\n" | ./busybox-armv7l nc -l -p 55764 | tr '\n' '#' | sed "s/.*##//" | tr '#' '\n'
    done
    

    它不起作用,即它只是打印出所有内容:

    POST / HTTP/1.1
    Content-Type: text/plain;charset=UTF-8
    Content-Length: 5
    Connection: Keep-Alive
    Accept-Encoding: gzip
    Accept-Language: en,*
    User-Agent: Mozilla/5.0
    Host: 127.0.0.1:55764
    
    HelloPOST / HTTP/1.1
    Content-Type: text/plain;charset=UTF-8
    Content-Length: 5
    Connection: Keep-Alive
    Accept-Encoding: gzip
    Accept-Language: en,*
    User-Agent: Mozilla/5.0
    Host: 127.0.0.1:55764
    
    Hello
    

    cat 但不是用 数控 ?

    1 回复  |  直到 7 年前
        1
  •  1
  •   gopy    7 年前

    nc的输出转到stderr,只需添加 & | 要使管道有效,请执行以下操作:

    echo -e "HTTP/1.1 200 OK\n\n" | ./busybox-armv7l nc -l -p 55764 |& tr '\n' '#' | sed "s/.*##//" | tr '#' '\n

    推荐文章