Linux script with curl to check webservice is up

后端 未结 6 837
难免孤独
难免孤独 2021-01-30 17:05

I have a webservice provided at http://localhost/test/testweb

I want to write a script to check if webservice is up with curl

If there a curl parame

6条回答
  •  独厮守ぢ
    2021-01-30 17:38

    I needed a better answer to this, so I wrote the script below.

    The fakePhrase is used to detect ISP "Search Assist" adware HTTP resposnes.

    #!/bin/bash
    
    fakePhrase="verizon"
    siteList=(
      'http://google.com'
      'https://google.com'
      'http://wikipedia.org'
      'https://wikipedia.org'
      'http://cantgettherefromhere'
      'http://searchassist.verizon.com'
    )
    
    exitStatus=0
    
    function isUp {
      http=`curl -sL -w "%{http_code}" "$1" -o temp_isUp`
      fakeResponse=`cat temp_isUp | grep $fakePhrase`
      if [ -n "$fakeResponse" ]; then
        http=$fakePhrase
      fi
      case $http in
      [2]*)
        ;;
      [3]*)
        echo 'Redirect'
        ;;
      [4]*)
        exitStatus=4
        echo "$1 is DENIED with ${http}"
        ;;
      [5]*)
        exitStatus=5
        echo "$1 is ERROR with ${http}"
        ;;
      *)
        exitStatus=6
        echo "$1 is NO RESPONSE with ${http}"
        ;;
      esac
    }
    
    for var in "${siteList[@]}"
    do
      isUp $var
    done
    
    if [ "$exitStatus" -eq "0" ]; then
      echo 'All up'
    fi
    
    rm temp_isUp
    exit $exitStatus
    

提交回复
热议问题