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)
要使 telnet 连接成功后立即退出,请故意传递未知的 telnet 选项并测试退出代码是否为 48:
\ncurl --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\nRun Code Online (Sandbox Code Playgroud)\n我们故意将一个未知的 telnet 选项传递BOGUS=1给curl 的-t, --telnet-option选项。替换BOGUS为除 、 或 三个受支持选项之外的TTYPE任何XDISPLOC名称NEW_ENV。
48 是curl 的“ CURLE_UNKNOWN_OPTION (48)传递给libcurl 的选项无法识别/已知”的错误代码。
\n这是有效的,因为只有在成功连接后才会处理 telnet 选项。
\n故意传递语法错误的 telnet 选项,例如BOGUS或 空字符串,并测试退出代码 49 ( CURLE_SETOPT_OPTION_SYNTAX )。我们在以下函数中使用这种方法。和以前一样,这是有效的,因为curl 仅在成功连接后才处理 telnet 选项。
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\nRun Code Online (Sandbox Code Playgroud)\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} \nRun Code Online (Sandbox Code Playgroud)\nhttps://curl.se/docs/todo.html#exit_immediately_upon_connection
\n通过: https: //curl.se/mail/archive-2022-04/0027.html
小智 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)
| 归档时间: |
|
| 查看次数: |
2457 次 |
| 最近记录: |