如何连接到在Docker容器中运行的Netcat? [英] How to connect to netcat running in docker container?

查看:73
本文介绍了如何连接到在Docker容器中运行的Netcat?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找用于发送请求和执行方案的小型休息服务器.

I'm looking small rest server for sent request and execute scenario.

我在这里找到它:使用netcat nc创建最小的REST Web服务器

我正在尝试执行此小型Rest服务器.

I'm trying to execute this small rest server.

在Dockerfile和bash脚本下方.

Below Dockerfile and bash script.

Dockerfile

Dockerfile

FROM debian
ADD  ./rest.sh /rest.sh
RUN apt-get update \
  && DEBIAN_FRONTEND=noninteractive \
  && apt-get install -y net-tools netcat curl \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/* \
  && chmod +x /rest.sh
EXPOSE 80
CMD /rest.sh

rest.sh

#!/bin/bash
/bin/rm -f out
/usr/bin/mkfifo  out
trap "rm -f out" EXIT
while true
do
  /bin/cat out | /bin/nc -l 80 > >( # parse the netcat output, to build the answer redirected to the pipe "out".
    export REQUEST=
    while read line
    do
      line=$(echo "$line" | tr -d '[\r\n]')
      if /bin/echo "$line" | grep -qE '^GET /' # if line starts with "GET /"
      then
        REQUEST=$(echo "$line" | cut -d ' ' -f2) # extract the request
      elif [ "x$line" = x ] # empty line / end of request
      then
        HTTP_200="HTTP/1.1 200 OK"
        HTTP_LOCATION="Location:"
        HTTP_404="HTTP/1.1 404 Not Found"
        # call a script here
        # Note: REQUEST is exported, so the script can parse it (to answer 200/403/404 status code + content)
        if echo $REQUEST | grep -qE '^/echo/'
        then
            printf "%s\n%s %s\n\n%s\n" "$HTTP_200" "$HTTP_LOCATION" $REQUEST ${REQUEST#"/echo/"} > out
        elif echo $REQUEST | grep -qE '^/date'
        then
            date > out
        elif echo $REQUEST | grep -qE '^/stats'
        then
            vmstat -S M > out
        elif echo $REQUEST | grep -qE '^/net'
        then
            ifconfig > out
        else
            printf "%s\n%s %s\n\n%s\n" "$HTTP_404" "$HTTP_LOCATION" $REQUEST "Resource $REQUEST NOT FOUND!" > out
        fi
      fi
    done
  )
done

  1. docker build -t ncimange.

docker run -d -i -p 80:80 --name ncrest ncimange

docker容器ls

IMAGE               COMMAND                 CREATED             STATUS              PORTS                NAMES
ncimange            "/bin/sh -c /rest.sh"   8 seconds ago       Up 2 seconds        0.0.0.0:80->80/tcp   ncrest

  • docker ps

    IMAGE               COMMAND                 CREATED             STATUS              PORTS                NAMES
    ncimange            "/bin/sh -c /rest.sh"   41 seconds ago      Up 34 seconds       0.0.0.0:80->80/tcp   ncrest
    

  • docker日志ncrest

    来自主机:

    curl -i http://127.0.0.1:80/date
    
    curl: (56) Recv failure: Connection reset by peer
    

  • 从容器中:

  • From container:

    docker exec -it ncrest /bin/bash
    
    netstat -an|grep LISTEN
    tcp        0      0 0.0.0.0:41783           0.0.0.0:*               LISTEN
    
    curl -i http://127.0.0.1:80/date
    curl: (7) Failed to connect to 127.0.0.1 port 80: Connection refused
    
    curl -i http://127.0.0.1:41783/date
    curl: (56) Recv failure: Connection reset by peer
    

  • 如何连接到netcat rest docker容器?

    How to connect to netcat rest docker container?

    推荐答案

    您正在安装错误的" netcat.Debian有两个netcat软件包: netcat-traditional netcat-openbsd ,两者都略有不同. netcat 软件包是 netcat-traditional 的别名.

    You are installing the "wrong" netcat. Debian has two netcat packages: netcat-traditional and netcat-openbsd, and both are slighty different. The netcat package is an alias of netcat-traditional.

    例如,在您的情况下,您的nc命令应为 nc -l -p 80 ,因为 nc -l 80 仅适用于 netcat-openbsd.

    For example, in your case your nc command should be nc -l -p 80, because nc -l 80 will only work on netcat-openbsd.

    tl; dr:如果要使用未修改的脚本,请安装 netcat-openbsd 而不是ǹetcat.

    tl;dr: install netcat-openbsd instead of ǹetcat if you wish to use your script unmodified..

    这篇关于如何连接到在Docker容器中运行的Netcat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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