telnet 后关闭curl 连接

Chr*_*ton 6 containers curl telnet

我想要的是:

连接成功后,我希望curl能够成功退出。我在容器内运行此命令,因此我希望curl 命令能够成功退出,以便容器也能成功退出。

这是我的例子:

$ curl -v telnet://google.com:443/
*   Trying 172.217.197.113...
* TCP_NODELAY set
* Connected to google.com (172.217.197.113) port 443 (#0)
Run Code Online (Sandbox Code Playgroud)

我尝试过的选项:

没有保持活力:

$ curl -v --no-keepalive telnet://google.com:443/
*   Trying 172.217.197.102...
* TCP_NODELAY set
* Connected to google.com (172.217.197.102) port 443 (#0)
Run Code Online (Sandbox Code Playgroud)

连接超时:

$ curl -v --connect-timeout 5 telnet://google.com:443/
*   Trying 172.217.197.139...
* TCP_NODELAY set
* Connected to google.com (172.217.197.139) port 443 (#0)
Run Code Online (Sandbox Code Playgroud)

保持存活时间:

$ curl -v --keepalive-time 5 telnet://google.com:443/
*   Trying 172.217.197.139...
* TCP_NODELAY set
* Connected to google.com (172.217.197.139) port 443 (#0)
Run Code Online (Sandbox Code Playgroud)

标志定义

--no-keepalive (Disable keepalive use on the connection)

--connect-timeout (SECONDS Maximum time allowed for connection)

--keepalive-time (SECONDS Wait SECONDS between keepalive probes)

Rob*_*ade 6

要使 telnet 连接成功后立即退出,请故意传递未知的 telnet 选项并测试退出代码是否为 48:

\n
curl --telnet-option \'BOGUS=1\' --connect-timeout 2 -s telnet://google.com:443 </dev/null\ncode=$?\nif [ "$?" -eq 48 ]; then\n  echo "Telnet connection was successful"\nelse\n  echo "Telnet connection failed. Curl exit code was $code"\nfi\n
Run Code Online (Sandbox Code Playgroud)\n

我们故意将一个未知的 telnet 选项传递BOGUS=1给curl 的-t, --telnet-option选项。替换BOGUS为除 、 或 三个受支持选项之外的TTYPE任何XDISPLOC名称NEW_ENV

\n

48 是curl 的CURLE_UNKNOWN_OPTION (48)传递给libcurl 的选项无法识别/已知”的错误代码。

\n

这是有效的,因为只有在成功连接后才会处理 telnet 选项。

\n

略有变化

\n

故意传递语法错误的 telnet 选项,例如BOGUS或 空字符串,并测试退出代码 49 ( CURLE_SETOPT_OPTION_SYNTAX )。我们在以下函数中使用这种方法。和以前一样,这是有效的,因为curl 仅在成功连接后才处理 telnet 选项。

\n

形式化为函数

\n
curl --telnet-option \'BOGUS=1\' --connect-timeout 2 -s telnet://google.com:443 </dev/null\ncode=$?\nif [ "$?" -eq 48 ]; then\n  echo "Telnet connection was successful"\nelse\n  echo "Telnet connection failed. Curl exit code was $code"\nfi\n
Run Code Online (Sandbox Code Playgroud)\n

测试

\n
# Args:\n# 1 - host\n# 2 - port\ntcp_port_is_open() {\n   local code\n   curl --telnet-option BOGUS --connect-timeout 2 -s telnet://"$1:$2" </dev/null\n   code=$?\n   case $code in\n     49) return 0 ;;\n     *) return "$code" ;;\n   esac\n} \n
Run Code Online (Sandbox Code Playgroud)\n

编辑:2022 年 5 月 27 日:观看此待办事项

\n

https://curl.se/docs/todo.html#exit_immediately_upon_connection
\n通过: https: //curl.se/mail/archive-2022-04/0027.html

\n


小智 0

对我有用的是向 telnet 发送“exit”,并且由于您对输出不感兴趣,因此将其重定向

$ echo "exit" | curl --connect-timeout 1 -s telnet://google.com:443 > /dev/null
$ echo $?
0
$ echo "exit" | curl --connect-timeout 1 -s telnet://google.com:4433 > /dev/null
$ echo $?
7
Run Code Online (Sandbox Code Playgroud)