如何在shell脚本中从curl WebSocket接口提取响应状态代码

velaa5lx  于 2023-08-05  发布在  Shell
关注(0)|答案(2)|浏览(115)
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为空,结果为空。
请告诉我为什么?

mfuanj7w

mfuanj7w1#

而不是

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)
b=$(echo $result | awk '/^HTTP/{print $2}')
echo $b

字符串
你可以试试这个:

# quote result of command substitution
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)

# quote result of command substitution 
b="$(echo "$result" | grep -o -e '^< HTTP/1\.1 \([0-9]\{3\}\)')"

echo "$b"
# should produce "< HTTP/1.1 101"

echo "${b## *}"
# should produce "101"


希望能帮上忙。

rjee0c15

rjee0c152#

curl  -o txt.txt --insecure      --no-buffer --no-keepalive      --header "Connection: Upgrade"      --header "Upgrade: websocket"      --header "Sec-WebSocket-Version: 13"  --header "Sec-WebSocket-Key: websocket"  -v -i ${array[i]}
result=$(cat txt.txt | awk '/^HTTP/{print $2}')
echo $result

字符串
我用txt.txt文件保存结果,然后用awk分析结果代码。
我用它工作。

相关问题