skip to Main Content
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

i use this to check websocket interface success or fail
when i execute this, the result is below:

* 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=

I want to extract the http status code 101 in the shell script
How to do, please help, thanks

I try some other way.use 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}'

it can show the 101,
but when i use it into shell script, but it not work
my shell script is below:


#!/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

the variable b is empty and result is empty.
please tell me why?

2

Answers


  1. Chosen as BEST ANSWER
    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
    

    I use a txt.txt file to save the result,then use awk analysis the result code.
    I use it work.


  2. Instead of

    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
    

    … you can try this:

    # 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"
    

    Hope that helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search