help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to know /dev/tcp/host/port is closed?


From: Peng Yu
Subject: How to know /dev/tcp/host/port is closed?
Date: Wed, 31 Mar 2021 16:17:01 -0500

See the following code. When the first printf is finished, the
connection should have been closed. But it seems that bash does not
know it (see the exit status code of { true >&"$fd"; } 2>/dev/null).
Is it a bug?

Since the connection is closed in the middle of the code block, is {
... } {fd}<>/dev/tcp/httpbin.org/80 not appropriate to be used in this
case? Thanks.

$ {
    header=(
        'GET /get HTTP/1.1'
        'Host: httpbin.org'
        'Connection: close'
    )
    builtin printf '%s\r\n' "${header[@]}" '' >&"$fd"
    IFS= read -d '' -r -t 3 response <&"$fd"
    builtin printf '>>>%s\n' "$response"
    { true >&"$fd"; } 2>/dev/null
    echo ">>>$?"
    builtin printf '%s\r\n' "${header[@]}" '' >&"$fd"
    IFS= read -d '' -r -t 3 response <&"$fd"
    builtin printf '>>>%s\n' "$response"
} {fd}<>/dev/tcp/httpbin.org/80
>>>HTTP/1.1 200 OK
Date: Wed, 31 Mar 2021 21:12:26 GMT
Content-Type: application/json
Content-Length: 196
Connection: close
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

{
  "args": {},
  "headers": {
    "Host": "httpbin.org",
    "X-Amzn-Trace-Id": "Root=1-6064e5ba-6625b466140cb6c01e47d6bc"
  },
  "origin": "108.59.0.37",
  "url": "http://httpbin.org/get";
}

>>>0
>>>

-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]