Skip to content

Instantly share code, notes, and snippets.

@rafaelfelix
Last active July 26, 2018 15:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rafaelfelix/5613539 to your computer and use it in GitHub Desktop.
Save rafaelfelix/5613539 to your computer and use it in GitHub Desktop.
Shell script to programmatically check if an IP address belongs to Akamai. Requires Akamai login, password and permission to access https://control.akamai.com/partner-tools/index.action?target=VerifyAkamaiIP
#!/bin/bash
#
# Issue request to Akamai tool verifyAkamaiIpInternal to check if
# the given IP address belongs to Akamai
#
## defining helper functions first
# show usage
show_usage() {
echo "usage: $0 -i <IP_ADDR> -u <AKAMAI_USERNAME> -p <AKAMAI_PASSWORD>"
}
# creates a temporary file (using an OS-aware way)
create_tempfile() {
local OS=$(uname -s)
local TMP_FILENAME=""
case $OS in
Darwin)
TMP_FILENAME=$(mktemp -t a)
;;
Linux|*)
TMP_FILENAME=$(mktemp)
;;
esac
echo $TMP_FILENAME
}
# helper function to encode url data (see http://stackoverflow.com/questions/296536/urlencode-from-a-bash-script)
rawurlencode() {
local string="${1}"
local strlen=${#string}
local encoded=""
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
echo "${encoded}" # You can either set a return variable (FASTER)
REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p
}
## MAIN
# ARGS parse
while getopts ":i:u:p:" opt; do
case $opt in
i)
IP_ADDR=$OPTARG
;;
u)
AKAMAI_USERNAME=$OPTARG
;;
p)
AKAMAI_PASSWORD=$OPTARG
;;
?)
show_usage
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
show_usage
exit 1
;;
esac
done
# test for required args
if [ -z $IP_ADDR ] || [ -z $AKAMAI_USERNAME ] || [ -z $AKAMAI_PASSWORD ]; then
show_usage
exit 1
fi
AKAMAI_BASEURL='https://control.akamai.com'
AKAMAI_LOGIN_ACTION=${AKAMAI_BASEURL}'/EdgeAuth/asyncUserLogin'
AKAMAI_VERIFYIP_ACTION=${AKAMAI_BASEURL}'/partner-tools/rest/tools/v1.0/verifycdnip'
CURL_COOKIE_JAR=$(create_tempfile)
ENCODED_USR=$(rawurlencode $AKAMAI_USERNAME)
ENCODED_PWD=$(rawurlencode $AKAMAI_PASSWORD)
# get TARGET_URL hash from Akamai
TARGET_URL_HASH=$(curl -s -I "${AKAMAI_VERIFYIP_ACTION}" | grep TARGET_URL | awk -FTARGET_URL= '{print $2}')
# auth
curl -s -H 'Content-type: application/x-www-form-urlencoded' \
-c $CURL_COOKIE_JAR \
-d "username=${ENCODED_USR}&password=${ENCODED_PWD}&TARGET_URL=${TARGET_URL_HASH}" \
${AKAMAI_LOGIN_ACTION}
# request IP info
IP_INFO=$(curl -s -H 'Content-type: application/x-www-form-urlencoded' \
-b $CURL_COOKIE_JAR \
"${AKAMAI_VERIFYIP_ACTION}?ip=${IP_ADDR}")
# TODO: Better error handling
if [ $? -ne 0 ]; then
echo 'ERROR'
exit $?
fi
# removes cookie jar file
rm -f ${CURL_COOKIE_JAR}
echo $IP_INFO
exit 0
@rafaelfelix
Copy link
Author

After Akamai deployed the new version of their control panel (LUNA Control Center), my script stopped working. I've just updated it with the new URL and changed the param akamaiIp to ip. Worked like a charm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment