Skip to content

Instantly share code, notes, and snippets.

@Knight1
Last active May 3, 2018 04:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Knight1/1b2edbe40c8ba4938a4bd70cfc847bc7 to your computer and use it in GitHub Desktop.
Save Knight1/1b2edbe40c8ba4938a4bd70cfc847bc7 to your computer and use it in GitHub Desktop.
Check Packet.net availability of specific server type in every location and ~💵 not collected per month
#!/bin/bash
#Usage: default is Type 2a. If you wish another server type use ./packet.net 1/2/3
#See https://www.packet.net/bare-metal/
#Obtain api key here => https://app.packet.net/portal#/api-keys
KEY=
if [[ -z ${1+x} ]]; then
PLAN=2a
else
PLAN=$1
fi
echo "Checking availability of Type "${PLAN}
for i in {1..4}
do
#Locations
if [[ ${i} -eq 1 ]]; then
FACILITY=ams1
elif [[ ${i} -eq 2 ]]; then
FACILITY=nrt1
elif [[ ${i} -eq 3 ]]; then
FACILITY=sjc1
elif [[ ${i} -eq 4 ]]; then
FACILITY=ewr1
else
echo "[CRIT] Aborting Facility error"
exit 1
fi
if [[ ${1} = "0" ]]; then
PRICE=33
elif [[ ${1} = "1" ]]; then
PRICE=268
elif [[ ${1} = "2" ]]; then
PRICE=840
elif [[ ${1} = "3" ]]; then
PRICE=1176
else
PRICE=744
fi
#Quantity Check
QUANTITY=50
FIRSTRUN=true
while (true); do
#curl
RETURN=`curl \
-X POST \
-d '{"servers":[{"$ref":"#\/definitions\/ServerInfo","facility":"'"$FACILITY"'","plan":"baremetal_'"$PLAN"'","quantity":"'"$QUANTITY"'"}]}' \
-H "X-Auth-Token: $KEY" \
-H "Content-Type: application/json" \
-s -o /dev/null -w "%{http_code}" \
https://api.packet.net/capacity`
if [ ${RETURN} -eq 503 ]; then
unset FIRSTRUN
if [ -z ${UP+x} ]; then
QUANTITY=$((QUANTITY / 2))
else
QUANTITY=$((QUANTITY - 1))
TOTAL=$((QUANTITY + TOTAL))
PRICETOTAL=$((QUANTITY * PRICE))
PRICETOTALEND=$((PRICETOTALEND + PRICETOTAL))
echo ${QUANTITY} "Available Servers in" ${FACILITY}
ARRAY[$FACILITY]=${QUANTITY}
unset UP
unset PRICETOTAL
break
fi
elif [ ${RETURN} -eq 204 ]; then
QUANTITY=$((QUANTITY + 1))
UP=1
if [ -z ${FIRSTRUN+x} ]; then
#do nothing
continue
else
QUANTITY=$((QUANTITY + 10))
fi
else
echo "[CRIT] Aborting; Maybe you forgot the API key"
exit 1
fi
done
done
echo "Total Servers: "$TOTAL
echo "Total Price: "$PRICETOTALEND
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment