curl --insecure \
--no-buffer \
--header "Connection: Upgrade" \
--header "Upgrade: websocket" \
--header "Sec-WebSocket-Version: 13" \
--header "Sec-WebSocket-Key: websocket" \
-v \
https://127.0.0.1:443/blackhole
字符串
我用它来检查WebSocket接口是否成功
当我执行这个时,结果如下:
* About to connect() to 127.0.0.1 port 443 (#0)
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* skipping SSL peer certificate verification
* SSL connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
* Server certificate:
* subject: CN=CloudFlare Origin Certificate,OU=CloudFlare Origin CA,O="CloudFlare, Inc."
* start date: May 15 13:49:00 2022 GMT
* expire date: May 11 13:49:00 2037 GMT
* common name: CloudFlare Origin Certificate
* issuer: ST=California,L=San Francisco,OU=CloudFlare Origin SSL Certificate Authority,O="CloudFlare, Inc.",C=US
> GET /blackhole HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 127.0.0.1
> Accept: */*
> Connection: Upgrade
> Upgrade: websocket
> Sec-WebSocket-Version: 13
> Sec-WebSocket-Key: websocket
>
< HTTP/1.1 101 Switching Protocols
< Server: nginx/1.18.0
< Date: Wed, 05 Jul 2023 06:13:59 GMT
< Connection: upgrade
< Upgrade: websocket
< Sec-WebSocket-Accept: qVby4apnn2tTYtB1nPPVYUn68gY=
型
我想提取shell脚本中的http状态码101
怎么办,请帮帮忙,谢谢
我尝试了一些其他的方法。使用awk。
curl --insecure --no-buffer --header "Connection: Upgrade" --header "Upgrade: websocket" --header "Sec-WebSocket-Version: 13" --header "Sec-WebSocket-Key: websocket" -v -i https://127.0.0.1:443/blackhole | awk '/^HTTP/{print $2}'
型
它可以显示101,
但是当我把它用在shell脚本中时,却不起作用
我的shell脚本如下:
#!/bin/bash
result=$(curl --insecure --no-buffer --header "Connection: Upgrade" --header "Upgrade: websocket" --header "Sec-WebSocket-Version: 13" --header "Sec-WebSocket-Key: websocket" -v -i --silent https://127.0.0.1:443/blackhole)
#echo $result
b=$(echo $result | awk '/^HTTP/{print $2}')
echo $b
if [[ "$b" =101 ]]; then
echo "hello world"
fi
型
变量b为空,结果为空。
请告诉我为什么?
2条答案
按热度按时间mfuanj7w1#
而不是
字符串
你可以试试这个:
型
希望能帮上忙。
rjee0c152#
字符串
我用txt.txt文件保存结果,然后用awk分析结果代码。
我用它工作。