Created
August 13, 2013 06:34
-
-
Save jorgevila/6218399 to your computer and use it in GitHub Desktop.
Script to check Connectivity from a File containing several URLs for Rest Services
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# | |
# @author: jorge.vila@gmail.com | |
# 2013 | |
if [ $# -ne 1 ] | |
then | |
echo "Usage: checkGWConnectivity.sh [FILE]" | |
fi | |
FILE=$1 | |
TIMEOUT=5 | |
# | |
# Parses URL | |
# @return proto | |
# @return user | |
# @return pass | |
# @return host | |
# @return port | |
# @return path | |
# | |
function parse_url { | |
# extract the protocol | |
proto="`echo $1 | grep '://' | sed -e's,^\(.*://\).*,\1,g'`" | |
# remove the protocol | |
url=`echo $1 | sed -e s,$proto,,g` | |
# extract the user and password (if any) | |
userpass="`echo $url | grep @ | cut -d@ -f1`" | |
pass=`echo $userpass | grep : | cut -d: -f2` | |
if [ -n "$pass" ]; then | |
user=`echo $userpass | grep : | cut -d: -f1` | |
else | |
user=$userpass | |
fi | |
#Default Port Value | |
port=80 | |
if [ "https" == $proto ] | |
then | |
port=443 | |
fi | |
# extract the host -- updated | |
hostport=`echo $url | sed -e s,$userpass@,,g | cut -d/ -f1` | |
port=`echo $hostport | grep : | cut -d: -f2` | |
if [ -n "$port" ]; then | |
host=`echo $hostport | grep : | cut -d: -f1` | |
else | |
host=$hostport | |
#Default Port Value | |
port=80 | |
if [ "https" == $proto ] | |
then | |
port=443 | |
fi | |
fi | |
# extract the path (if any) | |
path="`echo $url | grep / | cut -d/ -f2-`" | |
} | |
# | |
# Test URL for all HTTP METHODS | |
# | |
function check_url { | |
for HTTP_METHOD in GET POST PUT DELETE HEAD | |
do | |
if curl --silent --head --connect-timeout $TIMEOUT -X $HTTP_METHOD $1 | egrep "HTTP/1.[0-1]" >/dev/null | |
then | |
echo "$HTTP_METHOD $1 OK" | |
else | |
echo "$HTTP_METHOD $1 ERROR" | |
fi | |
done | |
} | |
# GET URLs from PATH | |
TMP=$(mktemp) | |
grep http $FILE | sed 's/\\//g' > $TMP | |
while read URL_TO_TEST | |
do | |
echo "#--------------------" | |
echo $URL_TO_TEST | |
parse_url $URL_TO_TEST | |
nc -w $TIMEOUT -z $host $port && { echo "$host $port up" && check_url $URL_TO_TEST; } || { echo "$host $port ERROR" >&2; } | |
done < $TMP | |
rm $TMP | |
#((ping -w5 -c3 8.8.8.8 || ping -w5 -c3 4.2.2.1) > /dev/null 2>&1) && echo "up" || (echo "down" && exit 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment