Created
October 2, 2020 09:39
-
-
Save IIPoliII/7f8943037e477692e265806a2051b8c6 to your computer and use it in GitHub Desktop.
Get monthly used bandwidth Hetzner Robot API
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 | |
#Created by Poli | |
user="YourUserHere" | |
password="YourPasswordHere" | |
first_date=$(date -d "`date +%Y%m01`" +%Y-%m-%d) | |
last_date=$(date -d "`date +%Y%m01` +1 month -1 day" +%Y-%m-%d) | |
UsedBandwidth=0 | |
ips=`curl -su "${user}:${password}" https://robot-ws.your-server.de/ip.json | jq '.[].ip.ip' | sed -e 's/^"//' -e 's/"$//'` | |
subnets=`curl -su "${user}:${password}" https://robot-ws.your-server.de/subnet.json | jq '.[].subnet.ip' | sed -e 's/^"//' -e 's/"$//'` | |
command="curl -su \"${user}:${password}\" https://robot-ws.your-server.de/traffic --data-urlencode 'type=month' --data-urlencode \"from=${first_date}\" --data-urlencode \"to=${last_date}\"" | |
while IFS= read -r line ; | |
do | |
command="$command --data-urlencode \"ip[]=${line}\"" | |
done <<< "$ips" | |
while IFS= read -r line ; | |
do | |
command="$command --data-urlencode \"subnet[]=${line}\"" | |
done <<< "$subnets" | |
CommandOutput=`eval ${command} | jq '.[].data[].sum'` | |
while IFS= read -r line ; | |
do | |
UsedBandwidth=`echo "$UsedBandwidth + $line" | bc` | |
done <<< "$CommandOutput" | |
echo "Total used bandwidth this month : $(printf %0.2f $UsedBandwidth) GB" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment